Skip to content

Commit 892f482

Browse files
committed
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.
1 parent bf9a1c8 commit 892f482

File tree

5 files changed

+69
-0
lines changed

5 files changed

+69
-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
@@ -792,6 +792,7 @@ fn test_unstable_options_tracking_hash() {
792792
tracked!(instrument_xray, Some(InstrumentXRay::default()));
793793
tracked!(link_directives, false);
794794
tracked!(link_only, true);
795+
tracked!(llvm_module_flag, Some("bar:u32:123:max"));
795796
tracked!(llvm_plugins, vec![String::from("plugin_name")]);
796797
tracked!(location_detail, LocationDetail { file: true, line: false, column: false });
797798
tracked!(maximal_hir_to_mir_coverage, true);

compiler/rustc_session/src/options.rs

+31
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,8 @@ mod desc {
421421
pub const parse_proc_macro_execution_strategy: &str =
422422
"one of supported execution strategies (`same-thread`, or `cross-thread`)";
423423
pub const parse_dump_solver_proof_tree: &str = "one of: `always`, `on-request`, `on-error`";
424+
425+
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`)";
424426
}
425427

426428
mod parse {
@@ -1263,6 +1265,33 @@ mod parse {
12631265
};
12641266
true
12651267
}
1268+
1269+
pub(crate) fn parse_llvm_module_flag(
1270+
slot: &mut Vec<(String, u32, String)>,
1271+
v: Option<&str>,
1272+
) -> bool {
1273+
let elements = v.unwrap_or_default().split(':').collect::<Vec<_>>();
1274+
let [key, md_type, value, behavior] = elements.as_slice() else {
1275+
return false;
1276+
};
1277+
if *md_type != "u32" {
1278+
// Currently we only support u32 metadata flags, but require the
1279+
// type for forward-compatibility.
1280+
return false;
1281+
}
1282+
let Ok(value) = value.parse::<u32>() else {
1283+
return false;
1284+
};
1285+
let behavior = behavior.to_lowercase();
1286+
let all_behaviors =
1287+
["error", "warning", "require", "override", "append", "appendunique", "max", "min"];
1288+
if !all_behaviors.contains(&behavior.as_str()) {
1289+
return false;
1290+
}
1291+
1292+
slot.push((key.to_string(), value, behavior));
1293+
true
1294+
}
12661295
}
12671296

12681297
options! {
@@ -1574,6 +1603,8 @@ options! {
15741603
"link native libraries in the linker invocation (default: yes)"),
15751604
link_only: bool = (false, parse_bool, [TRACKED],
15761605
"link the `.rlink` file generated by `-Z no-link` (default: no)"),
1606+
llvm_module_flag: Vec<(String, u32, String)> = (Vec::new(), parse_llvm_module_flag, [TRACKED],
1607+
"a list of module flags to pass to LLVM (space separated)"),
15771608
llvm_plugins: Vec<String> = (Vec::new(), parse_list, [TRACKED],
15781609
"a list LLVM plugins to enable (space separated)"),
15791610
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:appendunique
3+
4+
fn main() {}
5+
6+
// CHECK: !{i32 1, !"foo", i32 123}
7+
// CHECK: !{i32 6, !"bar", i32 42}

0 commit comments

Comments
 (0)