Skip to content

Commit bac15db

Browse files
committed
Emit basic XRay instrumentation attributes
Add the attributes to functions according to the settings. "xray-always" overrides "xray-never", and they both override "xray-ignore-loops" and "xray-instruction-threshold", but we'll let lints deal with warnings about silly attribute combinations.
1 parent b3cadd2 commit bac15db

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

compiler/rustc_codegen_llvm/src/attributes.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,34 @@ fn instrument_function_attr<'ll>(cx: &CodegenCx<'ll, '_>) -> SmallVec<[&'ll Attr
134134
&mcount_name,
135135
));
136136
}
137+
if let Some(options) = &cx.sess().opts.unstable_opts.instrument_xray {
138+
// XRay instrumentation is similar to __cyg_profile_func_{enter,exit}.
139+
// Function prologue and epilogue are instrumented with NOP sleds,
140+
// a runtime library later replaces them with detours into tracing code.
141+
if options.always {
142+
attrs.push(llvm::CreateAttrStringValue(cx.llcx, "function-instrument", "xray-always"));
143+
}
144+
if options.never {
145+
attrs.push(llvm::CreateAttrStringValue(cx.llcx, "function-instrument", "xray-never"));
146+
}
147+
if options.ignore_loops {
148+
attrs.push(llvm::CreateAttrString(cx.llcx, "xray-ignore-loops"));
149+
}
150+
// LLVM will not choose the default for us, but rather requires specific
151+
// threshold in absence of "xray-always". Use the same default as Clang.
152+
let threshold = options.instruction_threshold.unwrap_or(200);
153+
attrs.push(llvm::CreateAttrStringValue(
154+
cx.llcx,
155+
"xray-instruction-threshold",
156+
&threshold.to_string(),
157+
));
158+
if options.skip_entry {
159+
attrs.push(llvm::CreateAttrString(cx.llcx, "xray-skip-entry"));
160+
}
161+
if options.skip_exit {
162+
attrs.push(llvm::CreateAttrString(cx.llcx, "xray-skip-exit"));
163+
}
164+
}
137165
attrs
138166
}
139167

0 commit comments

Comments
 (0)