Repository navigation
Refactor LLVM export into focused modules - #67
mohamedsamirx wants to merge 5 commits into
Conversation
Signed-off-by: Mohamed Samir <[email protected]>
Signed-off-by: Mohamed Samir <[email protected]>
Signed-off-by: Mohamed Samir <[email protected]>
Signed-off-by: Mohamed Samir <[email protected]>
Signed-off-by: Mohamed Samir <[email protected]>
|
Hey, just a heads up: this issue was assigned to me, meaning I was already working on it. It would've been good to reach out first before opening a PR, to avoid duplicating effort. For future contributions, please check whether an issue is already assigned before jumping in. I'll leave it to the maintainers to decide how to proceed from here. |
|
Hey @fbrv , first of all, take it easy, it's not that big of a deal. When I saw the issue, I didn't see an assignee. You opened the issue, but I didn't know you were working on it. So you can easily ignore my pull request, close it, and push your work. I don't mind. Life is easier than that, man. Have a good day. |
|
Here you are i closed it myself , even feel free to copy my whole branch and re push it if you want. |
|
@mohamedsamirx sorry for the first message, that was uncalled for on my end. Fair enough, if the assignee wasn't visible that's on the tooling, not you. |
|
Whether it's on the tool or on me, trust me, it doesn't mean anything to me. I'm not targeting pull requests, and I wasn't trying to take anything from you. If the issue was assigned to you, then it is yours. I just had some free time, and if you have already done the work, please go ahead and push it. I really don't mind at all. I already learned something along the way, and that's what matters. A message to the maintainers: please do not merge my pull request. Take his instead — he deserves it. |
There was a problem hiding this comment.
So I'm not super sure I would have done in this way, for me the biggest pain point here is from L48, how I envisioned the change was more something like
enum LlvmOp<'a> {
Return(&'a ops::ReturnOp),
Unreachable,
Br,
CondBr,
Load(&'a ops::LoadOp),
Store,
AtomicLoad(&'a ops::AtomicLoadOp),
// ... one variant per op
}
impl<'a> LlvmOp<'a> {
fn from_op(op_obj: &'a dyn Op) -> Option<Self> {
if let Some(op) = op_obj.downcast_ref::<ops::ReturnOp>() { return Some(Self::Return(op)); }
if let Some(op) = op_obj.downcast_ref::<ops::LoadOp>() { return Some(Self::Load(op)); }
// ...
None
}
}Then the dispatch in export_op becomes:
match LlvmOp::from_op(op_obj.as_ref()) {
Some(LlvmOp::Return(op)) => self.export_return_op(op_ref, op, value_names, output)?,
Some(LlvmOp::Load(op)) => self.export_load_op(op_ref, op, value_names, output)?,
Some(LlvmOp::AtomicLoad(op)) => self.export_atomic_load_op(op_ref, op, value_names, output)?,
// ...
None => writeln!(output, " ; Unknown op: {op_id}").unwrap(),
}The annoying bit is that if we have to add another op we would need to update the match case on both side but we can eliminate most of the changes with a macro like
macro_rules! classify_op {
($op_obj:expr, { $($Variant:ident => $OpTy:ty),* $(,)? }) => {{
let op = $op_obj;
$(if let Some(inner) = op.downcast_ref::<$OpTy>() { return Some(Self::$Variant(inner)); })*
None
}};
}Also I would keep ops a single file, like llvm AsmWriter
Summary
This PR addresses #59 by refactoring the
dialect-llvmLLVM IR export code into focused modules without changing generated IR.The export implementation had grown into large files, especially the operation emission path. This splits the code by responsibility so future changes to metadata, calls, inline asm, memory ops, atomics, and operation-specific emission are easier to review and maintain.
Changes include:
export/config.rs.export/externs.rs.export/literals.rs.export/metadata.rs.export/names.rs.export/type_printing.rs.aggregate.rsarithmetic.rsasm.rsatomic.rscalls.rscasts.rsmemory.rsterminator.rsvirtual_ops.rsop_emission/mod.rsfocused on operation dispatch.crates/dialect-llvm/README.mdwith the new source layout.Motivation
Closes #59.
The goal is intentionally conservative: improve maintainability and make the LLVM export pipeline easier to evolve, while
preserving current behavior exactly.
This should make the next phases easier to implement as smaller, reviewable PRs:
readonly,nocapture, ornoalias.Behavior Preservation
This PR is intended to be a refactor-only change.
I compared representative generated output before and after the refactor using:
Comparison base:
Results:
Hashes matched before and after for each artifact:
base vecadd.ll: c5a626c41b602ff9b305f3f08b7fca1c05b7491fad12cfe8412b4b062490eb34
current vecadd.ll: c5a626c41b602ff9b305f3f08b7fca1c05b7491fad12cfe8412b4b062490eb34
base vecadd.ptx: c1a3420703760a196aa2b0591941ad42cb270cfbe5599566be4fb5e55cc98248
current vecadd.ptx: c1a3420703760a196aa2b0591941ad42cb270cfbe5599566be4fb5e55cc98248
Testing
cargo fmt --check
git diff --check
cargo test -p dialect-llvm
cargo clippy -p dialect-llvm -- -D warnings
cargo check
cargo clippy --workspace -- -D warnings
cargo oxide run vecadd --arch sm_75
The vecadd example completed successfully:
SUCCESS: All 1024 elements correct!