Skip to content

Commit b5e038d

Browse files
committed
Auto merge of #157048 - JonathanBrouwer:rollup-2S3ck2Y, r=JonathanBrouwer
Rollup of 3 pull requests Successful merges: - #156568 (Fix Xtensa vaarg stack transition algorithm) - #156659 (coverage: Build the expansion tree around `SyntaxContext`) - #156941 (Offload: Update confusing and outdated file name)
2 parents 59bc26c + 2456df1 commit b5e038d

8 files changed

Lines changed: 74 additions & 69 deletions

File tree

compiler/rustc_codegen_llvm/src/back/write.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -805,10 +805,10 @@ pub(crate) unsafe fn llvm_optimize(
805805
if cgcx.target_is_like_gpu && config.offload.contains(&config::Offload::Device) {
806806
let device_path = cgcx.output_filenames.path(OutputType::Object);
807807
let device_dir = device_path.parent().unwrap();
808-
let device_out = device_dir.join("host.out");
808+
let device_out = device_dir.join("device.bin");
809809
let device_out_c = path_to_c_string(device_out.as_path());
810810
unsafe {
811-
// 1) Bundle device module into offload image host.out (device TM)
811+
// 1) Bundle device module into offload image device.bin (device TM)
812812
let ok = llvm::LLVMRustBundleImages(
813813
module.module_llvm.llmod(),
814814
module.module_llvm.tm.raw(),
@@ -821,7 +821,7 @@ pub(crate) unsafe fn llvm_optimize(
821821
}
822822

823823
// This assumes that we previously compiled our kernels for a gpu target, which created a
824-
// `host.out` artifact. The user is supposed to provide us with a path to this artifact, we
824+
// `device.bin` artifact. The user is supposed to provide us with a path to this artifact, we
825825
// don't need any other artifacts from the previous run. We will embed this artifact into our
826826
// LLVM-IR host module, to create a `host.o` ObjectFile, which we will write to disk.
827827
// The last, not yet automated steps uses the `clang-linker-wrapper` to process `host.o`.
@@ -837,7 +837,7 @@ pub(crate) unsafe fn llvm_optimize(
837837
} else if device_pathbuf
838838
.file_name()
839839
.and_then(|n| n.to_str())
840-
.is_some_and(|n| n != "host.out")
840+
.is_some_and(|n| n != "device.bin")
841841
{
842842
dcx.emit_err(crate::errors::OffloadWrongFileName);
843843
} else if !device_pathbuf.exists() {
@@ -846,14 +846,14 @@ pub(crate) unsafe fn llvm_optimize(
846846
let host_path = cgcx.output_filenames.path(OutputType::Object);
847847
let host_dir = host_path.parent().unwrap();
848848
let out_obj = host_dir.join("host.o");
849-
let host_out_c = path_to_c_string(device_pathbuf.as_path());
849+
let device_bin_c = path_to_c_string(device_pathbuf.as_path());
850850

851-
// 2) Finalize host: lib.bc + host.out -> host.o (host TM)
851+
// 2) Finalize host: lib.bc + device.bin -> host.o (host TM)
852852
// We create a full clone of our LLVM host module, since we will embed the device IR
853853
// into it, and this might break caching or incremental compilation otherwise.
854854
let llmod2 = llvm::LLVMCloneModule(module.module_llvm.llmod());
855855
let ok =
856-
unsafe { llvm::LLVMRustOffloadEmbedBufferInModule(llmod2, host_out_c.as_ptr()) };
856+
unsafe { llvm::LLVMRustOffloadEmbedBufferInModule(llmod2, device_bin_c.as_ptr()) };
857857
if !ok {
858858
dcx.emit_err(crate::errors::OffloadEmbedFailed);
859859
}
@@ -868,7 +868,7 @@ pub(crate) unsafe fn llvm_optimize(
868868
prof,
869869
true,
870870
);
871-
// We ignore cgcx.save_temps here and unconditionally always keep our `host.out` artifact.
871+
// We ignore cgcx.save_temps here and unconditionally always keep our `device.bin` artifact.
872872
// Otherwise, recompiling the host code would fail since we deleted that device artifact
873873
// in the previous host compilation, which would be confusing at best.
874874
}

compiler/rustc_codegen_llvm/src/errors.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,31 +58,33 @@ pub(crate) struct AutoDiffWithoutLto;
5858
pub(crate) struct AutoDiffWithoutEnable;
5959

6060
#[derive(Diagnostic)]
61-
#[diag("using the offload feature requires -Z offload=<Device or Host=/absolute/path/to/host.out>")]
61+
#[diag(
62+
"using the offload feature requires -Z offload=<Device or Host=/absolute/path/to/device.bin>"
63+
)]
6264
pub(crate) struct OffloadWithoutEnable;
6365

6466
#[derive(Diagnostic)]
6567
#[diag("using the offload feature requires -C lto=fat")]
6668
pub(crate) struct OffloadWithoutFatLTO;
6769

6870
#[derive(Diagnostic)]
69-
#[diag("using the `-Z offload=Host=/absolute/path/to/host.out` flag requires an absolute path")]
71+
#[diag("using the `-Z offload=Host=/absolute/path/to/device.bin` flag requires an absolute path")]
7072
pub(crate) struct OffloadWithoutAbsPath;
7173

7274
#[derive(Diagnostic)]
7375
#[diag(
74-
"using the `-Z offload=Host=/absolute/path/to/host.out` flag must point to a `host.out` file"
76+
"using the `-Z offload=Host=/absolute/path/to/device.bin` flag must point to a `device.bin` file"
7577
)]
7678
pub(crate) struct OffloadWrongFileName;
7779

7880
#[derive(Diagnostic)]
7981
#[diag(
80-
"the given path/file to `host.out` does not exist. Did you forget to run the device compilation first?"
82+
"the given path/file to `device.bin` does not exist. Did you forget to run the device compilation first?"
8183
)]
8284
pub(crate) struct OffloadNonexistingPath;
8385

8486
#[derive(Diagnostic)]
85-
#[diag("call to BundleImages failed, `host.out` was not created")]
87+
#[diag("call to BundleImages failed, `device.bin` was not created")]
8688
pub(crate) struct OffloadBundleImagesFailed;
8789

8890
#[derive(Diagnostic)]

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1693,15 +1693,15 @@ pub(crate) use self::Offload::*;
16931693
mod Offload {
16941694
use super::*;
16951695
unsafe extern "C" {
1696-
/// Processes the module and writes it in an offload compatible way into a "host.out" file.
1696+
/// Processes the module and writes it in an offload compatible way into a "device.bin" file.
16971697
pub(crate) fn LLVMRustBundleImages<'a>(
16981698
M: &'a Module,
16991699
TM: &'a TargetMachine,
1700-
host_out: *const c_char,
1700+
device_bin: *const c_char,
17011701
) -> bool;
17021702
pub(crate) unsafe fn LLVMRustOffloadEmbedBufferInModule<'a>(
17031703
_M: &'a Module,
1704-
_host_out: *const c_char,
1704+
_device_bin: *const c_char,
17051705
) -> bool;
17061706
pub(crate) fn LLVMRustOffloadMapper<'a>(
17071707
OldFn: &'a Value,
@@ -1717,19 +1717,19 @@ pub(crate) use self::Offload_fallback::*;
17171717
#[cfg(not(feature = "llvm_offload"))]
17181718
mod Offload_fallback {
17191719
use super::*;
1720-
/// Processes the module and writes it in an offload compatible way into a "host.out" file.
1720+
/// Processes the module and writes it in an offload compatible way into a "device.bin" file.
17211721
/// Marked as unsafe to match the real offload wrapper which is unsafe due to FFI.
17221722
#[allow(unused_unsafe)]
17231723
pub(crate) unsafe fn LLVMRustBundleImages<'a>(
17241724
_M: &'a Module,
17251725
_TM: &'a TargetMachine,
1726-
_host_out: *const c_char,
1726+
_device_bin: *const c_char,
17271727
) -> bool {
17281728
unimplemented!("This rustc version was not built with LLVM Offload support!");
17291729
}
17301730
pub(crate) unsafe fn LLVMRustOffloadEmbedBufferInModule<'a>(
17311731
_M: &'a Module,
1732-
_host_out: *const c_char,
1732+
_device_bin: *const c_char,
17331733
) -> bool {
17341734
unimplemented!("This rustc version was not built with LLVM Offload support!");
17351735
}

compiler/rustc_codegen_llvm/src/va_arg.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -994,7 +994,7 @@ fn emit_xtensa_va_arg<'ll, 'tcx>(
994994

995995
// let offset_next_corrected = offset_corrected + slot_size;
996996
// va_ndx = offset_next_corrected;
997-
let offset_next_corrected = bx.add(offset_next, bx.const_i32(slot_size));
997+
let offset_next_corrected = bx.add(offset_corrected, bx.const_i32(slot_size));
998998
// update va_ndx
999999
bx.store(offset_next_corrected, offset_ptr, ptr_align_abi);
10001000

compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ static Error writeFile(StringRef Filename, StringRef Data) {
178178

179179
// This is the first of many steps in creating a binary using llvm offload,
180180
// to run code on the gpu. Concrete, it replaces the following binary use:
181-
// clang-offload-packager -o host.out
181+
// clang-offload-packager -o device.bin
182182
// --image=file=device.bc,triple=amdgcn-amd-amdhsa,arch=gfx90a,kind=openmp
183183
// The input module is the rust code compiled for a gpu target like amdgpu.
184184
// Based on clang/tools/clang-offload-packager/ClangOffloadPackager.cpp

compiler/rustc_mir_transform/src/coverage/expansion.rs

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use itertools::Itertools;
22
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet, IndexEntry};
33
use rustc_middle::mir;
44
use rustc_middle::mir::coverage::{BasicCoverageBlock, BranchSpan};
5-
use rustc_span::{ExpnId, ExpnKind, Span};
5+
use rustc_span::{ExpnKind, Span, SyntaxContext};
66

77
use crate::coverage::from_mir;
88
use crate::coverage::graph::CoverageGraph;
@@ -17,31 +17,31 @@ pub(crate) struct SpanWithBcb {
1717

1818
#[derive(Debug)]
1919
pub(crate) struct ExpnTree {
20-
nodes: FxIndexMap<ExpnId, ExpnNode>,
20+
nodes: FxIndexMap<SyntaxContext, ExpnNode>,
2121
}
2222

2323
impl ExpnTree {
24-
pub(crate) fn get(&self, expn_id: ExpnId) -> Option<&ExpnNode> {
25-
self.nodes.get(&expn_id)
24+
pub(crate) fn get(&self, context: SyntaxContext) -> Option<&ExpnNode> {
25+
self.nodes.get(&context)
2626
}
2727
}
2828

2929
#[derive(Debug)]
3030
pub(crate) struct ExpnNode {
31-
/// Storing the expansion ID in its own node is not strictly necessary,
31+
/// Storing the syntax context in its own node is not strictly necessary,
3232
/// but is helpful for debugging and might be useful later.
3333
#[expect(dead_code)]
34-
pub(crate) expn_id: ExpnId,
34+
pub(crate) context: SyntaxContext,
3535
/// Index of this node in a depth-first traversal from the root.
3636
pub(crate) dfs_rank: usize,
3737

3838
// Useful info extracted from `ExpnData`.
3939
pub(crate) expn_kind: ExpnKind,
4040
/// Non-dummy `ExpnData::call_site` span.
4141
pub(crate) call_site: Option<Span>,
42-
/// Expansion ID of `call_site`, if present.
42+
/// Syntax context of `call_site`, if present.
4343
/// This links an expansion node to its parent in the tree.
44-
pub(crate) call_site_expn_id: Option<ExpnId>,
44+
pub(crate) call_site_context: Option<SyntaxContext>,
4545

4646
/// Holds the function signature span, if it belongs to this expansion.
4747
/// Used by special-case code in span refinement.
@@ -53,7 +53,7 @@ pub(crate) struct ExpnNode {
5353
/// Spans (and their associated BCBs) belonging to this expansion.
5454
pub(crate) spans: Vec<SpanWithBcb>,
5555
/// Expansions whose call-site is in this expansion.
56-
pub(crate) child_expn_ids: FxIndexSet<ExpnId>,
56+
pub(crate) child_contexts: FxIndexSet<SyntaxContext>,
5757
/// The "minimum" and "maximum" BCBs (in dominator order) of ordinary spans
5858
/// belonging to this tree node and all of its descendants. Used when
5959
/// creating a single code mapping representing an entire child expansion.
@@ -68,25 +68,25 @@ pub(crate) struct ExpnNode {
6868
}
6969

7070
impl ExpnNode {
71-
fn new(expn_id: ExpnId) -> Self {
72-
let expn_data = expn_id.expn_data();
71+
fn for_context(context: SyntaxContext) -> Self {
72+
let expn_data = context.outer_expn_data();
7373

7474
let call_site = Some(expn_data.call_site).filter(|sp| !sp.is_dummy());
75-
let call_site_expn_id = try { call_site?.ctxt().outer_expn() };
75+
let call_site_context = try { call_site?.ctxt() };
7676

7777
Self {
78-
expn_id,
78+
context,
7979
dfs_rank: usize::MAX,
8080

8181
expn_kind: expn_data.kind,
8282
call_site,
83-
call_site_expn_id,
83+
call_site_context,
8484

8585
fn_sig_span: None,
8686
body_span: None,
8787

8888
spans: vec![],
89-
child_expn_ids: FxIndexSet::default(),
89+
child_contexts: FxIndexSet::default(),
9090
minmax_bcbs: None,
9191

9292
branch_spans: vec![],
@@ -106,32 +106,32 @@ pub(crate) fn build_expn_tree(
106106
let raw_spans = from_mir::extract_raw_spans_from_mir(mir_body, graph);
107107

108108
let mut nodes = FxIndexMap::default();
109-
let new_node = |&expn_id: &ExpnId| ExpnNode::new(expn_id);
109+
let new_node = |&context: &SyntaxContext| ExpnNode::for_context(context);
110110

111111
for from_mir::RawSpanFromMir { raw_span, bcb } in raw_spans {
112112
let span_with_bcb = SpanWithBcb { span: raw_span, bcb };
113113

114114
// Create a node for this span's enclosing expansion, and add the span to it.
115-
let expn_id = span_with_bcb.span.ctxt().outer_expn();
116-
let node = nodes.entry(expn_id).or_insert_with_key(new_node);
115+
let context = span_with_bcb.span.ctxt();
116+
let node = nodes.entry(context).or_insert_with_key(new_node);
117117
node.spans.push(span_with_bcb);
118118

119119
// Now walk up the expansion call-site chain, creating nodes and registering children.
120-
let mut prev = expn_id;
121-
let mut curr_expn_id = node.call_site_expn_id;
122-
while let Some(expn_id) = curr_expn_id {
123-
let entry = nodes.entry(expn_id);
120+
let mut prev = context;
121+
let mut curr_context = node.call_site_context;
122+
while let Some(context) = curr_context {
123+
let entry = nodes.entry(context);
124124
let node_existed = matches!(entry, IndexEntry::Occupied(_));
125125

126126
let node = entry.or_insert_with_key(new_node);
127-
node.child_expn_ids.insert(prev);
127+
node.child_contexts.insert(prev);
128128

129129
if node_existed {
130130
break;
131131
}
132132

133-
prev = expn_id;
134-
curr_expn_id = node.call_site_expn_id;
133+
prev = context;
134+
curr_context = node.call_site_context;
135135
}
136136
}
137137

@@ -152,30 +152,30 @@ pub(crate) fn build_expn_tree(
152152
// If we have a span for the function signature, associate it with the
153153
// corresponding expansion tree node.
154154
if let Some(fn_sig_span) = hir_info.fn_sig_span
155-
&& let Some(node) = nodes.get_mut(&fn_sig_span.ctxt().outer_expn())
155+
&& let Some(node) = nodes.get_mut(&fn_sig_span.ctxt())
156156
{
157157
node.fn_sig_span = Some(fn_sig_span);
158158
}
159159

160160
// Also associate the body span with its expansion tree node.
161161
let body_span = hir_info.body_span;
162-
if let Some(node) = nodes.get_mut(&body_span.ctxt().outer_expn()) {
162+
if let Some(node) = nodes.get_mut(&body_span.ctxt()) {
163163
node.body_span = Some(body_span);
164164
}
165165

166166
// Associate each hole span (extracted from HIR) with its corresponding
167167
// expansion tree node.
168168
for &hole_span in &hir_info.hole_spans {
169-
let expn_id = hole_span.ctxt().outer_expn();
170-
let Some(node) = nodes.get_mut(&expn_id) else { continue };
169+
let context = hole_span.ctxt();
170+
let Some(node) = nodes.get_mut(&context) else { continue };
171171
node.hole_spans.push(hole_span);
172172
}
173173

174174
// Associate each branch span (recorded during MIR building) with its
175175
// corresponding expansion tree node.
176176
if let Some(coverage_info_hi) = mir_body.coverage_info_hi.as_deref() {
177177
for branch_span in &coverage_info_hi.branch_spans {
178-
if let Some(node) = nodes.get_mut(&branch_span.span.ctxt().outer_expn()) {
178+
if let Some(node) = nodes.get_mut(&branch_span.span.ctxt()) {
179179
node.branch_spans.push(BranchSpan::clone(branch_span));
180180
}
181181
}
@@ -188,17 +188,19 @@ pub(crate) fn build_expn_tree(
188188
///
189189
/// This allows subsequent operations to iterate over all nodes, while assuming
190190
/// that every node occurs before all of its descendants.
191-
fn sort_nodes_depth_first(nodes: &mut FxIndexMap<ExpnId, ExpnNode>) -> Result<(), MappingsError> {
192-
let mut dfs_stack = vec![ExpnId::root()];
191+
fn sort_nodes_depth_first(
192+
nodes: &mut FxIndexMap<SyntaxContext, ExpnNode>,
193+
) -> Result<(), MappingsError> {
194+
let mut dfs_stack = vec![SyntaxContext::root()];
193195
let mut next_dfs_rank = 0usize;
194-
while let Some(expn_id) = dfs_stack.pop() {
195-
if let Some(node) = nodes.get_mut(&expn_id) {
196+
while let Some(context) = dfs_stack.pop() {
197+
if let Some(node) = nodes.get_mut(&context) {
196198
node.dfs_rank = next_dfs_rank;
197199
next_dfs_rank += 1;
198-
dfs_stack.extend(node.child_expn_ids.iter().rev().copied());
200+
dfs_stack.extend(node.child_contexts.iter().rev().copied());
199201
}
200202
}
201-
nodes.sort_by_key(|_expn_id, node| node.dfs_rank);
203+
nodes.sort_by_key(|_context, node| node.dfs_rank);
202204

203205
// Verify that the depth-first search visited each node exactly once.
204206
for (i, &ExpnNode { dfs_rank, .. }) in nodes.values().enumerate() {
@@ -222,12 +224,12 @@ pub(crate) struct MinMaxBcbs {
222224
/// and the min/max of its immediate children.
223225
fn minmax_bcbs_for_expn_tree_node(
224226
graph: &CoverageGraph,
225-
nodes: &FxIndexMap<ExpnId, ExpnNode>,
227+
nodes: &FxIndexMap<SyntaxContext, ExpnNode>,
226228
node: &ExpnNode,
227229
) -> Option<MinMaxBcbs> {
228230
let immediate_span_bcbs = node.spans.iter().map(|sp: &SpanWithBcb| sp.bcb);
229231
let child_minmax_bcbs = node
230-
.child_expn_ids
232+
.child_contexts
231233
.iter()
232234
.flat_map(|id| nodes.get(id))
233235
.flat_map(|child| child.minmax_bcbs)

compiler/rustc_mir_transform/src/coverage/mappings.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ fn extract_branch_mappings(
8080

8181
// For now, ignore any branch span that was introduced by
8282
// expansion. This makes things like assert macros less noisy.
83-
let Some(node) = expn_tree.get(hir_info.body_span.ctxt().outer_expn()) else { return };
83+
let Some(node) = expn_tree.get(hir_info.body_span.ctxt()) else { return };
8484
if node.expn_kind != ExpnKind::Root {
8585
return;
8686
}

0 commit comments

Comments
 (0)