Skip to content

Make our DIFlags match LLVMDIFlags in the LLVM-C API #135156

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 2 commits into from
Jan 22, 2025

Conversation

Zalathar
Copy link
Contributor

@Zalathar Zalathar commented Jan 6, 2025

In order to be able to use a mixture of LLVM-C and C++ bindings for debuginfo, our Rust-side DIFlags needs to have the same layout as LLVM-C's LLVMDIFlags, and we also need to be able to convert it to the DIFlags accepted by LLVM's C++ API.

Internally, LLVM converts between the two types with a simple cast. We can't necessarily rely on that always being true, and LLVM doesn't expose a conversion function, so we have two potential options:

  • Convert each bit/subvalue individually
  • Statically assert that doing a cast is actually fine

As long as both types do remain the same under the hood (which seems likely), the static-assert-and-cast approach is easier and faster. If the static assertions ever start failing against some future version of LLVM, we'll have to switch over to the convert-each-subvalue approach, which is a bit more error-prone.


Extracted from #134009, though this PR ended up choosing the static-assert-and-cast approach over the convert-each-subvalue approach.

@rustbot
Copy link
Collaborator

rustbot commented Jan 6, 2025

r? @cuviper

rustbot has assigned @cuviper.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 6, 2025
@Zalathar
Copy link
Contributor Author

Zalathar commented Jan 6, 2025

cc @workingjubilee @klensy

I've managed to convince myself that this way is better than what I was doing in #134009.

Comparing all three values in a static assertion helps to guard against copy-paste errors, and we don't need to care about whether the C++ compiler is going to generate good or terrible code for the manual conversion.

@Zalathar Zalathar added the A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. label Jan 7, 2025
@@ -9,6 +9,8 @@ test = false
[dependencies]
# tidy-alphabetical-start
bitflags = "2.4.1"
# To avoid duplicate dependencies, this should match the version of gimli used
# by `rustc_codegen_ssa` via its `thorin-dwp` dependency.
Copy link
Member

Choose a reason for hiding this comment

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

This is off topic? (but harmless)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh yeah, this was a follow-up to #135115 that I tacked on to this PR due to it being trivial, and vaguely related to debuginfo bindings.

}

static_assert(eq(LLVMDIFlagZero, 0, DIFlags::FlagZero));
static_assert(eq(LLVMDIFlagPrivate, 1, DIFlags::FlagPrivate));
Copy link
Member

Choose a reason for hiding this comment

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

How about a good old C macro to make these less repetitive?

#define ASSERT_EQ_DI(Flag, Value) \
  static_assert((LLVMDI##Flag == (Value)) && (DIFlags::Flag == (Value)));

ASSERT_EQ_DI(FlagZero, 0);
ASSERT_EQ_DI(FlagPrivate, 1);
// etc.

I do see 1<<15 with different names below, but if that's the only one, then it could be stated explicitly. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

At the time I remember choosing to prefer plain C++ code over C macros, but your example does look like an improvement, so maybe I'll give it a try.

@Zalathar
Copy link
Contributor Author

(Rebased with no changes; I'll try out the C macro and push that if it looks like an improvement.)

@Zalathar
Copy link
Contributor Author

OK yeah, the C macro is a net improvement (diff).

It's less greppable, which is unfortunate, but the upside is that each individual line is compact enough to actually skim/read.

@cuviper
Copy link
Member

cuviper commented Jan 21, 2025

Looks good, thanks!

@bors r+

@bors
Copy link
Collaborator

bors commented Jan 21, 2025

📌 Commit d10bdaf has been approved by cuviper

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 21, 2025
bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 22, 2025
…iaskrgr

Rollup of 10 pull requests

Successful merges:

 - rust-lang#133372 (Refactor dyn-compatibility error and suggestions)
 - rust-lang#134396 (AIX: use align 8 for byval parameter)
 - rust-lang#135156 (Make our `DIFlags` match `LLVMDIFlags` in the LLVM-C API)
 - rust-lang#135816 (Use `structurally_normalize` instead of manual `normalizes-to` goals in alias relate errors)
 - rust-lang#135823 (make UI tests that use `--test` work on panic=abort targets)
 - rust-lang#135850 (Update the `wasm-component-ld` tool)
 - rust-lang#135858 (rustdoc: Finalize dyn compatibility renaming)
 - rust-lang#135866 (Don't pick `T: FnPtr` nested goals as the leaf goal in diagnostics for new solver)
 - rust-lang#135874 (Enforce that all spans are lowered in ast lowering)
 - rust-lang#135875 (Remove `Copy` bound from `enter_forall`)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit e0d74c0 into rust-lang:master Jan 22, 2025
6 checks passed
@rustbot rustbot added this to the 1.86.0 milestone Jan 22, 2025
@Zalathar Zalathar deleted the debuginfo-flags branch January 23, 2025 00:17
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants