Skip to content

Commit 0b24479

Browse files
committed
Auto merge of rust-lang#116555 - paulmenage:llvm-module-flag, r=wesleywiser
Add -Z llvm_module_flag Allow adding values to the `!llvm.module.flags` metadata for a generated module. The syntax is `-Z llvm_module_flag=<name>:<type>:<value>:<behavior>` Currently only u32 values are supported but the type is required to be specified for forward compatibility. The `behavior` element must match one of the named LLVM metadata behaviors.viors. This flag is expected to be perma-unstable.
2 parents d4559c0 + 2e6b575 commit 0b24479

File tree

5 files changed

+68
-0
lines changed

5 files changed

+68
-0
lines changed

compiler/rustc_codegen_llvm/src/context.rs

+18
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,24 @@ pub unsafe fn create_module<'ll>(
368368
llvm::LLVMMDNodeInContext(llcx, &name_metadata, 1),
369369
);
370370

371+
// Add module flags specified via -Z llvm_module_flag
372+
for (key, value, behavior) in &sess.opts.unstable_opts.llvm_module_flag {
373+
let key = format!("{key}\0");
374+
let behavior = match behavior.as_str() {
375+
"error" => llvm::LLVMModFlagBehavior::Error,
376+
"warning" => llvm::LLVMModFlagBehavior::Warning,
377+
"require" => llvm::LLVMModFlagBehavior::Require,
378+
"override" => llvm::LLVMModFlagBehavior::Override,
379+
"append" => llvm::LLVMModFlagBehavior::Append,
380+
"appendunique" => llvm::LLVMModFlagBehavior::AppendUnique,
381+
"max" => llvm::LLVMModFlagBehavior::Max,
382+
"min" => llvm::LLVMModFlagBehavior::Min,
383+
// We already checked this during option parsing
384+
_ => unreachable!(),
385+
};
386+
llvm::LLVMRustAddModuleFlag(llmod, behavior, key.as_ptr().cast(), *value)
387+
}
388+
371389
llmod
372390
}
373391

compiler/rustc_interface/src/tests.rs

+1
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,7 @@ fn test_unstable_options_tracking_hash() {
769769
tracked!(instrument_xray, Some(InstrumentXRay::default()));
770770
tracked!(link_directives, false);
771771
tracked!(link_only, true);
772+
tracked!(llvm_module_flag, vec![("bar".to_string(), 123, "max".to_string())]);
772773
tracked!(llvm_plugins, vec![String::from("plugin_name")]);
773774
tracked!(location_detail, LocationDetail { file: true, line: false, column: false });
774775
tracked!(maximal_hir_to_mir_coverage, true);

compiler/rustc_session/src/options.rs

+30
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,7 @@ mod desc {
429429
pub const parse_remap_path_scope: &str = "comma separated list of scopes: `macro`, `diagnostics`, `unsplit-debuginfo`, `split-debuginfo`, `split-debuginfo-path`, `object`, `all`";
430430
pub const parse_inlining_threshold: &str =
431431
"either a boolean (`yes`, `no`, `on`, `off`, etc), or a non-negative number";
432+
pub const parse_llvm_module_flag: &str = "<key>:<type>:<value>:<behavior>. Type must currently be `u32`. Behavior should be one of (`error`, `warning`, `require`, `override`, `append`, `appendunique`, `max`, `min`)";
432433
}
433434

434435
mod parse {
@@ -1331,6 +1332,33 @@ mod parse {
13311332
}
13321333
true
13331334
}
1335+
1336+
pub(crate) fn parse_llvm_module_flag(
1337+
slot: &mut Vec<(String, u32, String)>,
1338+
v: Option<&str>,
1339+
) -> bool {
1340+
let elements = v.unwrap_or_default().split(':').collect::<Vec<_>>();
1341+
let [key, md_type, value, behavior] = elements.as_slice() else {
1342+
return false;
1343+
};
1344+
if *md_type != "u32" {
1345+
// Currently we only support u32 metadata flags, but require the
1346+
// type for forward-compatibility.
1347+
return false;
1348+
}
1349+
let Ok(value) = value.parse::<u32>() else {
1350+
return false;
1351+
};
1352+
let behavior = behavior.to_lowercase();
1353+
let all_behaviors =
1354+
["error", "warning", "require", "override", "append", "appendunique", "max", "min"];
1355+
if !all_behaviors.contains(&behavior.as_str()) {
1356+
return false;
1357+
}
1358+
1359+
slot.push((key.to_string(), value, behavior));
1360+
true
1361+
}
13341362
}
13351363

13361364
options! {
@@ -1629,6 +1657,8 @@ options! {
16291657
"link native libraries in the linker invocation (default: yes)"),
16301658
link_only: bool = (false, parse_bool, [TRACKED],
16311659
"link the `.rlink` file generated by `-Z no-link` (default: no)"),
1660+
llvm_module_flag: Vec<(String, u32, String)> = (Vec::new(), parse_llvm_module_flag, [TRACKED],
1661+
"a list of module flags to pass to LLVM (space separated)"),
16321662
llvm_plugins: Vec<String> = (Vec::new(), parse_list, [TRACKED],
16331663
"a list LLVM plugins to enable (space separated)"),
16341664
llvm_time_trace: bool = (false, parse_bool, [UNTRACKED],
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# `llvm-module-flag`
2+
3+
---------------------
4+
5+
This flag allows adding a key/value to the `!llvm.module.flags` metadata in the
6+
LLVM-IR for a compiled Rust module. The syntax is
7+
8+
`-Z llvm_module_flag=<name>:<type>:<value>:<behavior>`
9+
10+
Currently only u32 values are supported but the type is required to be specified
11+
for forward compatibility. The `behavior` element must match one of the named
12+
LLVM [metadata behaviors](https://llvm.org/docs/LangRef.html#module-flags-metadata)

tests/codegen/llvm_module_flags.rs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Test for -Z llvm_module_flags
2+
// compile-flags: -Z llvm_module_flag=foo:u32:123:error -Z llvm_module_flag=bar:u32:42:max
3+
4+
fn main() {}
5+
6+
// CHECK: !{i32 1, !"foo", i32 123}
7+
// CHECK: !{i32 7, !"bar", i32 42}

0 commit comments

Comments
 (0)