Add codeview_annotation intrinsic#1
Conversation
instead of a string
de65083 to
6655f74
Compare
d13beed to
fbb804a
Compare
codeview annotation intrinsiccodeview_annotation intrinsic
671f701 to
9caa308
Compare
wesleywiser
left a comment
There was a problem hiding this comment.
In broad strokes, this looks reasonable to me.
My main question is in the second review comment regarding feature scope. The PR description seems a bit out of date in that a lot more than just string literals is supported and I think that is adding a lot of complexity to the implementation. If that isn't actually necessary (and if it is, why?), I think it would be better to simplify the feature to just string literals.
| let md_strings: Vec<&Metadata> = | ||
| strings.iter().map(|s| self.cx.create_metadata(s)).collect(); | ||
| let md_tuple = unsafe { | ||
| llvm::LLVMMDNodeInContext2(self.cx.llcx, md_strings.as_ptr(), md_strings.len()) |
There was a problem hiding this comment.
You should be able to call
rust/compiler/rustc_codegen_llvm/src/context.rs
Lines 1016 to 1017 in 00ca87d
| let mut operands = None; | ||
|
|
||
| match this.thir[id].kind { | ||
| // Inline array: e.g. &["lit1", STR_CONST, T::NAME] |
There was a problem hiding this comment.
To what extent is support for consts necessary? It seems like it's pretty significantly complicating the implementation vs only supporting string literals and as far as I know, __annotation does not support the equivalent C++ feature.
| #[unstable(feature = "codeview_annotation", issue = "none")] | ||
| #[rustc_intrinsic] | ||
| #[rustc_nounwind] | ||
| pub fn codeview_annotation(strings: &[&str]); |
There was a problem hiding this comment.
nit: probably should be &[&'static str] right?
| // Verifies that codeview_annotation intrinsic lowers correctly | ||
| // under various conditions like directly calling the intrinsic, | ||
| // calling through the macro, calling with different kinds of | ||
| // args and in the presence of duplicate calls. |
There was a problem hiding this comment.
Seems like there should be a test regarding the "blocks inlining" behavior.
|
Oh, I forgot to mention, I think you also need to adjust the MIR inliner to mirror the LLVM behavior of the annotation blocking inlining. |
This PR adds a new intrinsic called
codeview_annotationwhich lowers tollvm.codeview.annotationand allows users to emit arbitraryS_ANNOTATIONrecords into PDB files.Motivation
In Microsoft we are working on supporting Windows driver development in Rust.
codeview_annotationis needed to implement a feature called WPP tracing which requires strings from trace statements to be written to the PDB at compile time.Basically the user writes a trace statement like this in the driver:
which expands to a call to
codeview_annotation:and the string arguments get written to the PDB.
Later at runtime trace viewing tools extract those strings from the PDB, combine them with the stream of variables (e.g.
byte_countandelapsed_ms) coming out of the driver and produce a human readable log.This keeps tracing efficient by avoiding costly I/O of strings at runtime and also helps with not leaking implementation details of drivers.
The intrinsic could also be useful in general for other use cases where you need to embed some user-specific information in the PDB.
API
The intrinsic is declared as:
and is publicly exposed through a macro with the same name.
The
&[&str]parameter is required to be aconstas it must be available at compile time in order to be lowered tollvm.codeview.annotation. Non-constarguments are rejected with a compiler error.Supported Platforms
Only LLVM on msvc is supported. It is a no-op on other platforms.
Implementation
Calls to
codeview_annotationare lowered toStatementKind::Intrinsicat MIR building time. The string arguments are extracted from THIR and stored in the newly introduced variantNonDivergingIntrinsic::CodeviewAnnotation(Box<[Symbol]>)which is ultimately lowered tollvm.codeview.annotationin the backend.We have chosen to collect the string arguments at the time of THIR->MIR translation because it is easier to do so. The other option was to do it during codegen but that would have required walking chains of MIR locals backwards which was more complicated.
Future Work
Ideally we should have implemented this feature using const generics e.g.:
as that guarantees the compile time availability of the inputs. However, we are blocked on
adt_const_paramsand especiallyunsized_const_params. Whenever these features get stabilized we can update the implementation to make use of them.