-
Notifications
You must be signed in to change notification settings - Fork 640
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Connect new parser to LLVM bitcode backend (#269)
This is very incomplete. Just enough code to emit LLVM bitcode and continue further development
- Loading branch information
Showing
35 changed files
with
10,124 additions
and
7,507 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +0,0 @@ | ||
[build] | ||
rustflags = ["-C", "target-cpu=haswell"] | ||
|
||
[target."x86_64-pc-windows-gnu"] | ||
rustflags = ["-C", "link-self-contained=y", "-C", "target-cpu=haswell"] | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[package] | ||
name = "comgr" | ||
version = "0.0.0" | ||
authors = ["Andrzej Janik <[email protected]>"] | ||
edition = "2021" | ||
|
||
[lib] | ||
|
||
[dependencies] | ||
amd_comgr-sys = { path = "../ext/amd_comgr-sys" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
use amd_comgr_sys::*; | ||
use std::{ffi::CStr, mem, ptr}; | ||
|
||
struct Data(amd_comgr_data_t); | ||
|
||
impl Data { | ||
fn new( | ||
kind: amd_comgr_data_kind_t, | ||
name: &CStr, | ||
content: &[u8], | ||
) -> Result<Self, amd_comgr_status_s> { | ||
let mut data = unsafe { mem::zeroed() }; | ||
unsafe { amd_comgr_create_data(kind, &mut data) }?; | ||
unsafe { amd_comgr_set_data_name(data, name.as_ptr()) }?; | ||
unsafe { amd_comgr_set_data(data, content.len(), content.as_ptr().cast()) }?; | ||
Ok(Self(data)) | ||
} | ||
|
||
fn get(&self) -> amd_comgr_data_t { | ||
self.0 | ||
} | ||
|
||
fn copy_content(&self) -> Result<Vec<u8>, amd_comgr_status_s> { | ||
let mut size = unsafe { mem::zeroed() }; | ||
unsafe { amd_comgr_get_data(self.get(), &mut size, ptr::null_mut()) }?; | ||
let mut result: Vec<u8> = Vec::with_capacity(size); | ||
unsafe { result.set_len(size) }; | ||
unsafe { amd_comgr_get_data(self.get(), &mut size, result.as_mut_ptr().cast()) }?; | ||
Ok(result) | ||
} | ||
} | ||
|
||
struct DataSet(amd_comgr_data_set_t); | ||
|
||
impl DataSet { | ||
fn new() -> Result<Self, amd_comgr_status_s> { | ||
let mut data_set = unsafe { mem::zeroed() }; | ||
unsafe { amd_comgr_create_data_set(&mut data_set) }?; | ||
Ok(Self(data_set)) | ||
} | ||
|
||
fn add(&self, data: &Data) -> Result<(), amd_comgr_status_s> { | ||
unsafe { amd_comgr_data_set_add(self.get(), data.get()) } | ||
} | ||
|
||
fn get(&self) -> amd_comgr_data_set_t { | ||
self.0 | ||
} | ||
|
||
fn get_data( | ||
&self, | ||
kind: amd_comgr_data_kind_t, | ||
index: usize, | ||
) -> Result<Data, amd_comgr_status_s> { | ||
let mut data = unsafe { mem::zeroed() }; | ||
unsafe { amd_comgr_action_data_get_data(self.get(), kind, index, &mut data) }?; | ||
Ok(Data(data)) | ||
} | ||
} | ||
|
||
impl Drop for DataSet { | ||
fn drop(&mut self) { | ||
unsafe { amd_comgr_destroy_data_set(self.get()).ok() }; | ||
} | ||
} | ||
|
||
struct ActionInfo(amd_comgr_action_info_t); | ||
|
||
impl ActionInfo { | ||
fn new() -> Result<Self, amd_comgr_status_s> { | ||
let mut action = unsafe { mem::zeroed() }; | ||
unsafe { amd_comgr_create_action_info(&mut action) }?; | ||
Ok(Self(action)) | ||
} | ||
|
||
fn set_isa_name(&self, isa: &CStr) -> Result<(), amd_comgr_status_s> { | ||
let mut full_isa = "amdgcn-amd-amdhsa--".to_string().into_bytes(); | ||
full_isa.extend(isa.to_bytes_with_nul()); | ||
unsafe { amd_comgr_action_info_set_isa_name(self.get(), full_isa.as_ptr().cast()) } | ||
} | ||
|
||
fn get(&self) -> amd_comgr_action_info_t { | ||
self.0 | ||
} | ||
} | ||
|
||
impl Drop for ActionInfo { | ||
fn drop(&mut self) { | ||
unsafe { amd_comgr_destroy_action_info(self.get()).ok() }; | ||
} | ||
} | ||
|
||
pub fn compile_bitcode(gcn_arch: &CStr, buffer: &[u8]) -> Result<Vec<u8>, amd_comgr_status_s> { | ||
use amd_comgr_sys::*; | ||
let bitcode_data_set = DataSet::new()?; | ||
let bitcode_data = Data::new( | ||
amd_comgr_data_kind_t::AMD_COMGR_DATA_KIND_BC, | ||
c"zluda.bc", | ||
buffer, | ||
)?; | ||
bitcode_data_set.add(&bitcode_data)?; | ||
let reloc_data_set = DataSet::new()?; | ||
let action_info = ActionInfo::new()?; | ||
action_info.set_isa_name(gcn_arch)?; | ||
unsafe { | ||
amd_comgr_do_action( | ||
amd_comgr_action_kind_t::AMD_COMGR_ACTION_CODEGEN_BC_TO_RELOCATABLE, | ||
action_info.get(), | ||
bitcode_data_set.get(), | ||
reloc_data_set.get(), | ||
) | ||
}?; | ||
let exec_data_set = DataSet::new()?; | ||
unsafe { | ||
amd_comgr_do_action( | ||
amd_comgr_action_kind_t::AMD_COMGR_ACTION_LINK_RELOCATABLE_TO_EXECUTABLE, | ||
action_info.get(), | ||
reloc_data_set.get(), | ||
exec_data_set.get(), | ||
) | ||
}?; | ||
let executable = | ||
exec_data_set.get_data(amd_comgr_data_kind_t::AMD_COMGR_DATA_KIND_EXECUTABLE, 0)?; | ||
executable.copy_content() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[package] | ||
name = "amd_comgr-sys" | ||
version = "0.0.0" | ||
authors = ["Andrzej Janik <[email protected]>"] | ||
edition = "2021" | ||
links = "amd_comgr" | ||
|
||
[lib] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
bindgen --rust-target 1.77 /opt/rocm/include/amd_comgr/amd_comgr.h -o /tmp/amd_comgr.rs --no-layout-tests --default-enum-style=newtype --allowlist-function "amd_comgr.*" --allowlist-type "amd_comgr.*" --no-derive-debug --must-use-type amd_comgr_status_t --allowlist-var "^AMD_COMGR.*$" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
; | ||
; Definition file of amd_comgr_2.dll | ||
; Automatic generated by gendef | ||
; written by Kai Tietz 2008 | ||
; | ||
LIBRARY "amd_comgr_2.dll" | ||
EXPORTS | ||
amd_comgr_action_data_count | ||
amd_comgr_action_data_get_data | ||
amd_comgr_action_info_get_isa_name | ||
amd_comgr_action_info_get_language | ||
amd_comgr_action_info_get_logging | ||
amd_comgr_action_info_get_option_list_count | ||
amd_comgr_action_info_get_option_list_item | ||
amd_comgr_action_info_get_options | ||
amd_comgr_action_info_get_working_directory_path | ||
amd_comgr_action_info_set_isa_name | ||
amd_comgr_action_info_set_language | ||
amd_comgr_action_info_set_logging | ||
amd_comgr_action_info_set_option_list | ||
amd_comgr_action_info_set_options | ||
amd_comgr_action_info_set_working_directory_path | ||
amd_comgr_create_action_info | ||
amd_comgr_create_data | ||
amd_comgr_create_data_set | ||
amd_comgr_create_disassembly_info | ||
amd_comgr_create_symbolizer_info | ||
amd_comgr_data_set_add | ||
amd_comgr_data_set_remove | ||
amd_comgr_demangle_symbol_name | ||
amd_comgr_destroy_action_info | ||
amd_comgr_destroy_data_set | ||
amd_comgr_destroy_disassembly_info | ||
amd_comgr_destroy_metadata | ||
amd_comgr_destroy_symbolizer_info | ||
amd_comgr_disassemble_instruction | ||
amd_comgr_do_action | ||
amd_comgr_get_data | ||
amd_comgr_get_data_isa_name | ||
amd_comgr_get_data_kind | ||
amd_comgr_get_data_metadata | ||
amd_comgr_get_data_name | ||
amd_comgr_get_isa_count | ||
amd_comgr_get_isa_metadata | ||
amd_comgr_get_isa_name | ||
amd_comgr_get_mangled_name | ||
amd_comgr_get_metadata_kind | ||
amd_comgr_get_metadata_list_size | ||
amd_comgr_get_metadata_map_size | ||
amd_comgr_get_metadata_string | ||
amd_comgr_get_version | ||
amd_comgr_index_list_metadata | ||
amd_comgr_iterate_map_metadata | ||
amd_comgr_iterate_symbols | ||
amd_comgr_lookup_code_object | ||
amd_comgr_map_elf_virtual_address_to_code_object_offset | ||
amd_comgr_map_name_expression_to_symbol_name | ||
amd_comgr_metadata_lookup | ||
amd_comgr_populate_mangled_names | ||
amd_comgr_populate_name_expression_map | ||
amd_comgr_release_data | ||
amd_comgr_set_data | ||
amd_comgr_set_data_from_file_slice | ||
amd_comgr_set_data_name | ||
amd_comgr_status_string | ||
amd_comgr_symbol_get_info | ||
amd_comgr_symbol_lookup | ||
amd_comgr_symbolize |
Binary file not shown.
Oops, something went wrong.