This commit is contained in:
Joëlle van Essen 2025-09-23 18:07:05 -04:00 committed by GitHub
commit 4e89b3de3d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -138,11 +138,24 @@ impl<'a> DataSet<'a> {
Ok(()) Ok(())
} }
pub fn from_data(comgr: &'a Comgr, data: impl Iterator<Item = Data>) -> Result<Self, Error> {
let dataset = Self::new(comgr)?;
for data in data {
dataset.add(&data)?;
}
Ok(dataset)
}
fn get_data(&self, kind: DataKind, index: usize) -> Result<Data, Error> { fn get_data(&self, kind: DataKind, index: usize) -> Result<Data, Error> {
let mut handle = 0u64; let mut handle = 0u64;
call_dispatch!(self.comgr => amd_comgr_action_data_get_data(self, kind, { index }, { std::ptr::from_mut(&mut handle).cast() })); call_dispatch!(self.comgr => amd_comgr_action_data_get_data(self, kind, { index }, { std::ptr::from_mut(&mut handle).cast() }));
Ok(Data(handle)) Ok(Data(handle))
} }
fn get_content(&self, comgr: &Comgr, kind: DataKind, index: usize) -> Result<Vec<u8>, Error> {
self.get_data(kind, index)
.map(|data| data.copy_content(comgr))?
}
} }
struct Data(u64); struct Data(u64);
@ -196,28 +209,25 @@ pub fn compile_bitcode(
attributes_buffer: &[u8], attributes_buffer: &[u8],
compiler_hook: Option<&dyn Fn(&Vec<u8>, String)>, compiler_hook: Option<&dyn Fn(&Vec<u8>, String)>,
) -> Result<Vec<u8>, Error> { ) -> Result<Vec<u8>, Error> {
let bitcode_data_set = DataSet::new(comgr)?; let bitcode_data_set = DataSet::from_data(
let main_bitcode_data = Data::new(comgr, DataKind::Bc, c"zluda.bc", main_buffer)?; comgr,
bitcode_data_set.add(&main_bitcode_data)?; [
let stdlib_bitcode_data = Data::new(comgr, DataKind::Bc, c"ptx_impl.bc", ptx_impl)?; Data::new(comgr, DataKind::Bc, c"zluda.bc", main_buffer)?,
bitcode_data_set.add(&stdlib_bitcode_data)?; Data::new(comgr, DataKind::Bc, c"ptx_impl.bc", ptx_impl)?,
let attributes_bitcode_data = Data::new(comgr, DataKind::Bc, c"attributes.bc", attributes_buffer)?,
Data::new(comgr, DataKind::Bc, c"attributes.bc", attributes_buffer)?; ]
bitcode_data_set.add(&attributes_bitcode_data)?; .into_iter(),
)?;
let linking_info = ActionInfo::new(comgr)?; let linking_info = ActionInfo::new(comgr)?;
let linked_data_set = let linked_data_set =
comgr.do_action(ActionKind::LinkBcToBc, &linking_info, &bitcode_data_set)?; comgr.do_action(ActionKind::LinkBcToBc, &linking_info, &bitcode_data_set)?;
if let Some(hook) = compiler_hook { if let Some(hook) = compiler_hook {
// Run compiler hook on human-readable LLVM IR // Run compiler hook on human-readable LLVM IR
let data = linked_data_set.get_data(DataKind::Bc, 0)?; let data = linked_data_set.get_content(comgr, DataKind::Bc, 0)?;
let data = data.copy_content(comgr)?;
let data = ptx::bitcode_to_ir(data); let data = ptx::bitcode_to_ir(data);
hook(&data, String::from("linked.ll")); hook(&data, String::from("linked.ll"));
} }
let compile_to_exec = ActionInfo::new(comgr)?;
compile_to_exec.set_isa_name(gcn_arch)?;
compile_to_exec.set_language(Language::LlvmIr)?;
let common_options = [ let common_options = [
c"-mllvm", c"-mllvm",
c"-ignore-tti-inline-compatible", c"-ignore-tti-inline-compatible",
@ -252,32 +262,78 @@ pub fn compile_bitcode(
c"-inlinehint-threshold=3250", c"-inlinehint-threshold=3250",
] ]
}; };
compile_to_exec.set_options(common_options.chain(opt_options))?; let options = common_options.chain(opt_options);
let exec_data_set = comgr.do_action( let executable: Result<Vec<u8>, Error>;
ActionKind::CompileSourceToExecutable, if let Some(hook) = compiler_hook {
&compile_to_exec, // CompileSourceWithDeviceLibsToBc
let action_info = ActionInfo::new(comgr)?;
action_info.set_isa_name(gcn_arch)?;
action_info.set_language(Language::LlvmIr)?;
action_info.set_options(options.clone())?;
let dataset = comgr.do_action(
ActionKind::CompileSourceWithDeviceLibsToBc,
&action_info,
&linked_data_set, &linked_data_set,
)?; )?;
let executable = exec_data_set.get_data(DataKind::Executable, 0)?; let data = dataset.get_content(comgr, DataKind::Bc, 0)?;
let executable = executable.copy_content(comgr); let data = ptx::bitcode_to_ir(data);
if let Some(hook) = compiler_hook { hook(&data, String::from("with-device-libs.ll"));
// Run compiler hook for executable
// CodegenBcToRelocatable
let action_info = ActionInfo::new(comgr)?;
action_info.set_isa_name(gcn_arch)?;
action_info.set_options(options.clone())?;
let dataset =
comgr.do_action(ActionKind::CodegenBcToRelocatable, &action_info, &dataset)?;
let data = dataset.get_content(comgr, DataKind::Relocatable, 0)?;
hook(&data, String::from("relocatable.elf"));
// Disassemble relocatable
let action_info = ActionInfo::new(comgr)?;
action_info.set_isa_name(gcn_arch)?;
let disassembled_dataset = comgr.do_action(
ActionKind::DisassembleRelocatableToSource,
&action_info,
&dataset,
)?;
let disassembly = disassembled_dataset.get_content(comgr, DataKind::Source, 0)?;
hook(&disassembly, String::from("relocatable.asm"));
// LinkRelocatableToExecutable
let action_info = ActionInfo::new(comgr)?;
action_info.set_isa_name(gcn_arch)?;
let dataset = comgr.do_action(
ActionKind::LinkRelocatableToExecutable,
&action_info,
&dataset,
)?;
executable = dataset.get_content(comgr, DataKind::Executable, 0);
hook( hook(
executable.as_ref().unwrap_or(&Vec::new()), executable.as_ref().unwrap_or(&Vec::new()),
String::from("elf"), String::from("elf"),
); );
// Disassemble executable and run compiler hook // Disassemble executable
let action_info = ActionInfo::new(comgr)?; let action_info = ActionInfo::new(comgr)?;
action_info.set_isa_name(gcn_arch)?; action_info.set_isa_name(gcn_arch)?;
let disassembly = comgr.do_action( let disassembled_dataset = comgr.do_action(
ActionKind::DisassembleExecutableToSource, ActionKind::DisassembleExecutableToSource,
&action_info, &action_info,
&exec_data_set, &dataset,
)?; )?;
let disassembly = disassembly.get_data(DataKind::Source, 0)?; let disassembly = disassembled_dataset.get_content(comgr, DataKind::Source, 0)?;
let disassembly = disassembly.copy_content(comgr); hook(&disassembly, String::from("asm"));
hook(&disassembly.unwrap_or(Vec::new()), String::from("asm")) } else {
let compile_to_exec = ActionInfo::new(comgr)?;
compile_to_exec.set_isa_name(gcn_arch)?;
compile_to_exec.set_language(Language::LlvmIr)?;
compile_to_exec.set_options(options.clone())?;
let exec_data_set = comgr.do_action(
ActionKind::CompileSourceToExecutable,
&compile_to_exec,
&linked_data_set,
)?;
executable = exec_data_set.get_content(comgr, DataKind::Executable, 0);
} }
executable executable
} }
@ -299,14 +355,15 @@ pub fn get_symbols(comgr: &Comgr, elf: &[u8]) -> Result<Vec<(u32, String)>, Erro
} }
pub fn get_clang_version(comgr: &Comgr) -> Result<String, Error> { pub fn get_clang_version(comgr: &Comgr) -> Result<String, Error> {
let version_string_set = DataSet::new(comgr)?; let version_string_set = DataSet::from_data(
let version_string = Data::new( comgr,
iter::once(Data::new(
comgr, comgr,
DataKind::Source, DataKind::Source,
c"version.cpp", c"version.cpp",
b"__clang_version__", b"__clang_version__",
)?),
)?; )?;
version_string_set.add(&version_string)?;
let preprocessor_info = ActionInfo::new(comgr)?; let preprocessor_info = ActionInfo::new(comgr)?;
preprocessor_info.set_language(Language::Hip)?; preprocessor_info.set_language(Language::Hip)?;
preprocessor_info.set_options(iter::once(c"-P"))?; preprocessor_info.set_options(iter::once(c"-P"))?;
@ -315,9 +372,8 @@ pub fn get_clang_version(comgr: &Comgr) -> Result<String, Error> {
&preprocessor_info, &preprocessor_info,
&version_string_set, &version_string_set,
)?; )?;
let data = preprocessed.get_data(DataKind::Source, 0)?; let data = preprocessed.get_content(comgr, DataKind::Source, 0)?;
String::from_utf8(trim_whitespace_and_quotes(data.copy_content(comgr)?)?) String::from_utf8(trim_whitespace_and_quotes(data)?).map_err(|_| Error::UNKNOWN)
.map_err(|_| Error::UNKNOWN)
} }
// When running the preprocessor to expand the macro the output is surrounded by // When running the preprocessor to expand the macro the output is surrounded by
@ -543,6 +599,10 @@ impl_into!(
[ [
LinkBcToBc => AMD_COMGR_ACTION_LINK_BC_TO_BC, LinkBcToBc => AMD_COMGR_ACTION_LINK_BC_TO_BC,
CompileSourceToExecutable => AMD_COMGR_ACTION_COMPILE_SOURCE_TO_EXECUTABLE, CompileSourceToExecutable => AMD_COMGR_ACTION_COMPILE_SOURCE_TO_EXECUTABLE,
CompileSourceWithDeviceLibsToBc => AMD_COMGR_ACTION_COMPILE_SOURCE_WITH_DEVICE_LIBS_TO_BC,
CodegenBcToRelocatable => AMD_COMGR_ACTION_CODEGEN_BC_TO_RELOCATABLE,
DisassembleRelocatableToSource => AMD_COMGR_ACTION_DISASSEMBLE_RELOCATABLE_TO_SOURCE,
LinkRelocatableToExecutable => AMD_COMGR_ACTION_LINK_RELOCATABLE_TO_EXECUTABLE,
DisassembleExecutableToSource => AMD_COMGR_ACTION_DISASSEMBLE_EXECUTABLE_TO_SOURCE, DisassembleExecutableToSource => AMD_COMGR_ACTION_DISASSEMBLE_EXECUTABLE_TO_SOURCE,
SourceToPreprocessor => AMD_COMGR_ACTION_SOURCE_TO_PREPROCESSOR SourceToPreprocessor => AMD_COMGR_ACTION_SOURCE_TO_PREPROCESSOR
] ]