Skip to content

Refactor LLVM export into focused modules - #67

Closed
mohamedsamirx wants to merge 5 commits into
NVIDIA:mainfrom
mohamedsamirx:refactor-llvm-export-structure
Closed

mohamedsamirx wants to merge 5 commits into
NVIDIA:mainfrom
mohamedsamirx:refactor-llvm-export-structure

Conversation

@mohamedsamirx

Copy link
Copy Markdown
Contributor

Summary

This PR addresses #59 by refactoring the dialect-llvm LLVM 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:

  • Move backend export configuration into export/config.rs.
  • Move device extern declaration logic into export/externs.rs.
  • Move literal formatting into export/literals.rs.
  • Move NVVM metadata emission into export/metadata.rs.
  • Move symbol naming helpers into export/names.rs.
  • Move LLVM type printing into export/type_printing.rs.
  • Move function/block/value emission into focused modules.
  • Split operation emission into category modules:
    • aggregate.rs
    • arithmetic.rs
    • asm.rs
    • atomic.rs
    • calls.rs
    • casts.rs
    • memory.rs
    • terminator.rs
    • virtual_ops.rs
  • Keep op_emission/mod.rs focused on operation dispatch.
  • Update crates/dialect-llvm/README.md with 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:

  • Add sound LLVM call/function parameter attributes where justified, such as readonly, nocapture, or noalias.
  • Improve debug/tooling metadata for CUDA-GDB, NSYS, sanitizers, and DWARF-related workflows.
  • Add more focused export tests for specific op categories now that emission logic is separated.
  • Continue tightening operation-specific emission behavior without touching unrelated export paths.

Behavior Preservation

This PR is intended to be a refactor-only change.
I compared representative generated output before and after the refactor using:

cargo oxide run vecadd --arch sm_75

Comparison base:

Results:

  • vecadd.ll: exact match
  • vecadd.ptx: exact match
  • .ll line count: 102 before / 102 after
  • .ptx line count: 119 before / 119 after

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!

@fbrv

fbrv commented May 18, 2026 •

Copy link
Copy Markdown
Contributor

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.

@mohamedsamirx

Copy link
Copy Markdown
Contributor Author

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.

@mohamedsamirx

Copy link
Copy Markdown
Contributor Author

Here you are i closed it myself , even feel free to copy my whole branch and re push it if you want.

@fbrv

fbrv commented May 18, 2026

Copy link
Copy Markdown
Contributor

@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.
Please don't close the PR, you spent time on it and it deserves a proper review.

cc @mohamedsamirx

@mohamedsamirx

Copy link
Copy Markdown
Contributor Author

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Proposal: refactor dialect-llvm export.rs into fundamental building blocks

2 participants