Skip to content

Commit b81c6cd

Browse files
authored
Rollup merge of #83916 - Amanieu:asm_anonconst, r=petrochenkov
Use AnonConst for asm! constants This replaces the old system which used explicit promotion. See #83169 for more background. The syntax for `const` operands is still the same as before: `const <expr>`. Fixes #83169 Because the implementation is heavily based on inline consts, we suffer from the same issues: - We lose the ability to use expressions derived from generics. See the deleted tests in `src/test/ui/asm/const.rs`. - We are hitting the same ICEs as inline consts, for example #78174. It is unlikely that we will be able to stabilize this before inline consts are stabilized.
2 parents 4d5bb1c + 32be124 commit b81c6cd

File tree

37 files changed

+281
-242
lines changed

37 files changed

+281
-242
lines changed

Diff for: compiler/rustc_ast/src/ast.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1998,7 +1998,7 @@ pub enum InlineAsmOperand {
19981998
out_expr: Option<P<Expr>>,
19991999
},
20002000
Const {
2001-
expr: P<Expr>,
2001+
anon_const: AnonConst,
20022002
},
20032003
Sym {
20042004
expr: P<Expr>,

Diff for: compiler/rustc_ast/src/mut_visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1252,7 +1252,6 @@ pub fn noop_visit_expr<T: MutVisitor>(
12521252
match op {
12531253
InlineAsmOperand::In { expr, .. }
12541254
| InlineAsmOperand::InOut { expr, .. }
1255-
| InlineAsmOperand::Const { expr, .. }
12561255
| InlineAsmOperand::Sym { expr, .. } => vis.visit_expr(expr),
12571256
InlineAsmOperand::Out { expr, .. } => {
12581257
if let Some(expr) = expr {
@@ -1265,6 +1264,7 @@ pub fn noop_visit_expr<T: MutVisitor>(
12651264
vis.visit_expr(out_expr);
12661265
}
12671266
}
1267+
InlineAsmOperand::Const { anon_const, .. } => vis.visit_anon_const(anon_const),
12681268
}
12691269
}
12701270
}

Diff for: compiler/rustc_ast/src/visit.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -835,7 +835,6 @@ pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) {
835835
match op {
836836
InlineAsmOperand::In { expr, .. }
837837
| InlineAsmOperand::InOut { expr, .. }
838-
| InlineAsmOperand::Const { expr, .. }
839838
| InlineAsmOperand::Sym { expr, .. } => visitor.visit_expr(expr),
840839
InlineAsmOperand::Out { expr, .. } => {
841840
if let Some(expr) = expr {
@@ -848,6 +847,9 @@ pub fn walk_expr<'a, V: Visitor<'a>>(visitor: &mut V, expression: &'a Expr) {
848847
visitor.visit_expr(out_expr);
849848
}
850849
}
850+
InlineAsmOperand::Const { anon_const, .. } => {
851+
visitor.visit_anon_const(anon_const)
852+
}
851853
}
852854
}
853855
}

Diff for: compiler/rustc_ast_lowering/src/expr.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1411,9 +1411,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
14111411
out_expr: out_expr.as_ref().map(|expr| self.lower_expr_mut(expr)),
14121412
}
14131413
}
1414-
InlineAsmOperand::Const { ref expr } => {
1415-
hir::InlineAsmOperand::Const { expr: self.lower_expr_mut(expr) }
1416-
}
1414+
InlineAsmOperand::Const { ref anon_const } => hir::InlineAsmOperand::Const {
1415+
anon_const: self.lower_anon_const(anon_const),
1416+
},
14171417
InlineAsmOperand::Sym { ref expr } => {
14181418
hir::InlineAsmOperand::Sym { expr: self.lower_expr_mut(expr) }
14191419
}

Diff for: compiler/rustc_ast_pretty/src/pprust/state.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2149,10 +2149,10 @@ impl<'a> State<'a> {
21492149
None => s.word("_"),
21502150
}
21512151
}
2152-
InlineAsmOperand::Const { expr } => {
2152+
InlineAsmOperand::Const { anon_const } => {
21532153
s.word("const");
21542154
s.space();
2155-
s.print_expr(expr);
2155+
s.print_expr(&anon_const.value);
21562156
}
21572157
InlineAsmOperand::Sym { expr } => {
21582158
s.word("sym");

Diff for: compiler/rustc_builtin_macros/src/asm.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ fn parse_args<'a>(
136136
ast::InlineAsmOperand::InOut { reg, expr, late: true }
137137
}
138138
} else if p.eat_keyword(kw::Const) {
139-
let expr = p.parse_expr()?;
140-
ast::InlineAsmOperand::Const { expr }
139+
let anon_const = p.parse_anon_const_expr()?;
140+
ast::InlineAsmOperand::Const { anon_const }
141141
} else if p.eat_keyword(sym::sym) {
142142
let expr = p.parse_expr()?;
143143
match expr.kind {

Diff for: compiler/rustc_codegen_ssa/src/mir/block.rs

+30-34
Original file line numberDiff line numberDiff line change
@@ -822,41 +822,37 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
822822
InlineAsmOperandRef::InOut { reg, late, in_value, out_place }
823823
}
824824
mir::InlineAsmOperand::Const { ref value } => {
825-
if let mir::Operand::Constant(constant) = value {
826-
let const_value = self
827-
.eval_mir_constant(constant)
828-
.unwrap_or_else(|_| span_bug!(span, "asm const cannot be resolved"));
829-
let ty = constant.ty();
830-
let size = bx.layout_of(ty).size;
831-
let scalar = match const_value {
832-
ConstValue::Scalar(s) => s,
833-
_ => span_bug!(
834-
span,
835-
"expected Scalar for promoted asm const, but got {:#?}",
836-
const_value
837-
),
838-
};
839-
let value = scalar.assert_bits(size);
840-
let string = match ty.kind() {
841-
ty::Uint(_) => value.to_string(),
842-
ty::Int(int_ty) => {
843-
match int_ty.normalize(bx.tcx().sess.target.pointer_width) {
844-
ty::IntTy::I8 => (value as i8).to_string(),
845-
ty::IntTy::I16 => (value as i16).to_string(),
846-
ty::IntTy::I32 => (value as i32).to_string(),
847-
ty::IntTy::I64 => (value as i64).to_string(),
848-
ty::IntTy::I128 => (value as i128).to_string(),
849-
ty::IntTy::Isize => unreachable!(),
850-
}
825+
let const_value = self
826+
.eval_mir_constant(value)
827+
.unwrap_or_else(|_| span_bug!(span, "asm const cannot be resolved"));
828+
let ty = value.ty();
829+
let size = bx.layout_of(ty).size;
830+
let scalar = match const_value {
831+
ConstValue::Scalar(s) => s,
832+
_ => span_bug!(
833+
span,
834+
"expected Scalar for promoted asm const, but got {:#?}",
835+
const_value
836+
),
837+
};
838+
let value = scalar.assert_bits(size);
839+
let string = match ty.kind() {
840+
ty::Uint(_) => value.to_string(),
841+
ty::Int(int_ty) => {
842+
match int_ty.normalize(bx.tcx().sess.target.pointer_width) {
843+
ty::IntTy::I8 => (value as i8).to_string(),
844+
ty::IntTy::I16 => (value as i16).to_string(),
845+
ty::IntTy::I32 => (value as i32).to_string(),
846+
ty::IntTy::I64 => (value as i64).to_string(),
847+
ty::IntTy::I128 => (value as i128).to_string(),
848+
ty::IntTy::Isize => unreachable!(),
851849
}
852-
ty::Float(ty::FloatTy::F32) => f32::from_bits(value as u32).to_string(),
853-
ty::Float(ty::FloatTy::F64) => f64::from_bits(value as u64).to_string(),
854-
_ => span_bug!(span, "asm const has bad type {}", ty),
855-
};
856-
InlineAsmOperandRef::Const { string }
857-
} else {
858-
span_bug!(span, "asm const is not a constant");
859-
}
850+
}
851+
ty::Float(ty::FloatTy::F32) => f32::from_bits(value as u32).to_string(),
852+
ty::Float(ty::FloatTy::F64) => f64::from_bits(value as u64).to_string(),
853+
_ => span_bug!(span, "asm const has bad type {}", ty),
854+
};
855+
InlineAsmOperandRef::Const { string }
860856
}
861857
mir::InlineAsmOperand::SymFn { ref value } => {
862858
let literal = self.monomorphize(value.literal);

Diff for: compiler/rustc_hir/src/hir.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2347,7 +2347,7 @@ pub enum InlineAsmOperand<'hir> {
23472347
out_expr: Option<Expr<'hir>>,
23482348
},
23492349
Const {
2350-
expr: Expr<'hir>,
2350+
anon_const: AnonConst,
23512351
},
23522352
Sym {
23532353
expr: Expr<'hir>,

Diff for: compiler/rustc_hir/src/intravisit.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1189,7 +1189,6 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr<'v>)
11891189
match op {
11901190
InlineAsmOperand::In { expr, .. }
11911191
| InlineAsmOperand::InOut { expr, .. }
1192-
| InlineAsmOperand::Const { expr, .. }
11931192
| InlineAsmOperand::Sym { expr, .. } => visitor.visit_expr(expr),
11941193
InlineAsmOperand::Out { expr, .. } => {
11951194
if let Some(expr) = expr {
@@ -1202,6 +1201,9 @@ pub fn walk_expr<'v, V: Visitor<'v>>(visitor: &mut V, expression: &'v Expr<'v>)
12021201
visitor.visit_expr(out_expr);
12031202
}
12041203
}
1204+
InlineAsmOperand::Const { anon_const, .. } => {
1205+
visitor.visit_anon_const(anon_const)
1206+
}
12051207
}
12061208
}
12071209
}

Diff for: compiler/rustc_hir_pretty/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1570,10 +1570,10 @@ impl<'a> State<'a> {
15701570
None => s.word("_"),
15711571
}
15721572
}
1573-
hir::InlineAsmOperand::Const { expr } => {
1573+
hir::InlineAsmOperand::Const { anon_const } => {
15741574
s.word("const");
15751575
s.space();
1576-
s.print_expr(expr);
1576+
s.print_anon_const(anon_const);
15771577
}
15781578
hir::InlineAsmOperand::Sym { expr } => {
15791579
s.word("sym");

Diff for: compiler/rustc_middle/src/mir/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1213,7 +1213,7 @@ pub enum InlineAsmOperand<'tcx> {
12131213
out_place: Option<Place<'tcx>>,
12141214
},
12151215
Const {
1216-
value: Operand<'tcx>,
1216+
value: Box<Constant<'tcx>>,
12171217
},
12181218
SymFn {
12191219
value: Box<Constant<'tcx>>,

Diff for: compiler/rustc_middle/src/mir/visit.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -584,8 +584,7 @@ macro_rules! make_mir_visitor {
584584
} => {
585585
for op in operands {
586586
match op {
587-
InlineAsmOperand::In { value, .. }
588-
| InlineAsmOperand::Const { value } => {
587+
InlineAsmOperand::In { value, .. } => {
589588
self.visit_operand(value, location);
590589
}
591590
InlineAsmOperand::Out { place, .. } => {
@@ -607,7 +606,8 @@ macro_rules! make_mir_visitor {
607606
);
608607
}
609608
}
610-
InlineAsmOperand::SymFn { value } => {
609+
InlineAsmOperand::Const { value }
610+
| InlineAsmOperand::SymFn { value } => {
611611
self.visit_constant(value, location);
612612
}
613613
InlineAsmOperand::SymStatic { def_id: _ } => {}

Diff for: compiler/rustc_mir/src/borrow_check/invalidation.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,7 @@ impl<'cx, 'tcx> Visitor<'tcx> for InvalidationGenerator<'cx, 'tcx> {
204204
} => {
205205
for op in operands {
206206
match *op {
207-
InlineAsmOperand::In { reg: _, ref value }
208-
| InlineAsmOperand::Const { ref value } => {
207+
InlineAsmOperand::In { reg: _, ref value } => {
209208
self.consume_operand(location, value);
210209
}
211210
InlineAsmOperand::Out { reg: _, late: _, place, .. } => {
@@ -219,7 +218,8 @@ impl<'cx, 'tcx> Visitor<'tcx> for InvalidationGenerator<'cx, 'tcx> {
219218
self.mutate_place(location, out_place, Shallow(None), JustWrite);
220219
}
221220
}
222-
InlineAsmOperand::SymFn { value: _ }
221+
InlineAsmOperand::Const { value: _ }
222+
| InlineAsmOperand::SymFn { value: _ }
223223
| InlineAsmOperand::SymStatic { def_id: _ } => {}
224224
}
225225
}

Diff for: compiler/rustc_mir/src/borrow_check/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -734,8 +734,7 @@ impl<'cx, 'tcx> dataflow::ResultsVisitor<'cx, 'tcx> for MirBorrowckCtxt<'cx, 'tc
734734
} => {
735735
for op in operands {
736736
match *op {
737-
InlineAsmOperand::In { reg: _, ref value }
738-
| InlineAsmOperand::Const { ref value } => {
737+
InlineAsmOperand::In { reg: _, ref value } => {
739738
self.consume_operand(loc, (value, span), flow_state);
740739
}
741740
InlineAsmOperand::Out { reg: _, late: _, place, .. } => {
@@ -761,7 +760,8 @@ impl<'cx, 'tcx> dataflow::ResultsVisitor<'cx, 'tcx> for MirBorrowckCtxt<'cx, 'tc
761760
);
762761
}
763762
}
764-
InlineAsmOperand::SymFn { value: _ }
763+
InlineAsmOperand::Const { value: _ }
764+
| InlineAsmOperand::SymFn { value: _ }
765765
| InlineAsmOperand::SymStatic { def_id: _ } => {}
766766
}
767767
}

Diff for: compiler/rustc_mir/src/dataflow/move_paths/builder.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ impl<'b, 'a, 'tcx> Gatherer<'b, 'a, 'tcx> {
425425
for op in operands {
426426
match *op {
427427
InlineAsmOperand::In { reg: _, ref value }
428-
| InlineAsmOperand::Const { ref value } => {
428+
=> {
429429
self.gather_operand(value);
430430
}
431431
InlineAsmOperand::Out { reg: _, late: _, place, .. } => {
@@ -441,7 +441,8 @@ impl<'b, 'a, 'tcx> Gatherer<'b, 'a, 'tcx> {
441441
self.gather_init(out_place.as_ref(), InitKind::Deep);
442442
}
443443
}
444-
InlineAsmOperand::SymFn { value: _ }
444+
InlineAsmOperand::Const { value: _ }
445+
| InlineAsmOperand::SymFn { value: _ }
445446
| InlineAsmOperand::SymStatic { def_id: _ } => {}
446447
}
447448
}

Diff for: compiler/rustc_mir/src/transform/dest_prop.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -720,9 +720,6 @@ impl Conflicts<'a> {
720720
}
721721
}
722722
}
723-
InlineAsmOperand::Const { value } => {
724-
assert!(value.place().is_none());
725-
}
726723
InlineAsmOperand::InOut {
727724
reg: _,
728725
late: _,
@@ -731,6 +728,7 @@ impl Conflicts<'a> {
731728
}
732729
| InlineAsmOperand::In { reg: _, value: _ }
733730
| InlineAsmOperand::Out { reg: _, late: _, place: None }
731+
| InlineAsmOperand::Const { value: _ }
734732
| InlineAsmOperand::SymFn { value: _ }
735733
| InlineAsmOperand::SymStatic { def_id: _ } => {}
736734
}

0 commit comments

Comments
 (0)