Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Cranelift: fix `uadd_overflow` + load sinking. (#14272)

Chris Fallin84dbb9ffdc68
diff --git a/cranelift/codegen/src/machinst/lower.rs b/cranelift/codegen/src/machinst/lower.rs
index 1413fddb33..fc681f137f 100644
--- a/cranelift/codegen/src/machinst/lower.rs
+++ b/cranelift/codegen/src/machinst/lower.rs
@@ -1422,11 +1422,7 @@ fn compute_use_states(
let uses = |value| {
trace!(" -> pushing args for {} onto stack", value);
if let ValueDef::Result(src_inst, _) = f.dfg.value_def(value) {
- if is_value_use_root(f, src_inst) {
- None
- } else {
- Some(f.dfg.inst_values(src_inst))
- }
+ Some(f.dfg.inst_values(src_inst))
} else {
None
}
@@ -1486,26 +1482,6 @@ fn compute_use_states(
value_ir_uses
}
-/// Definition of a "root" instruction for the calculation of `ValueUseState`.
-///
-/// This function calculates whether `inst` is considered a "root" for value-use
-/// information. This concept is used to forcibly prevent looking-through the
-/// instruction during `get_value_as_source_or_const` as it additionally
-/// prevents propagating `Multiple`-used results of the `inst` here to the
-/// operands of the instruction.
-///
-/// Currently this is defined as multi-result instructions. That means that
-/// lowerings are never allowed to look through a multi-result instruction to
-/// generate patterns. Note that this isn't possible in ISLE today anyway so
-/// this isn't currently much of a loss.
-///
-/// The main purpose of this function is to prevent the operands of a
-/// multi-result instruction from being forcibly considered `Multiple`-used
-/// regardless of circumstances.
-fn is_value_use_root(f: &Function, inst: Inst) -> bool {
- f.dfg.inst_results(inst).len() > 1
-}
-
/// Function-level queries.
impl<'func, I: VCodeInst> Lower<'func, I> {
pub fn dfg(&self) -> &DataFlowGraph {
@@ -1710,16 +1686,7 @@ impl<'func, I: VCodeInst> Lower<'func, I> {
let src_side_effect = src_entry_color.get() != 0;
trace!(" -> src inst {}", self.f.dfg.display_inst(src_inst));
trace!(" -> has lowering side effect: {}", src_side_effect);
- if is_value_use_root(self.f, src_inst) {
- // If this instruction is a "root instruction" then it's
- // required that we can't look through it to see the
- // definition. This means that the `ValueUseState` for the
- // operands of this result assume that this instruction is
- // generated exactly once which might get violated were we
- // to allow looking through it.
- trace!(" -> is a root instruction");
- InputSourceInst::None
- } else if !src_side_effect {
+ if !src_side_effect {
// Otherwise if this instruction has no side effects and the
// value is used only once then we can look through it with
// a "unique" tag. A non-unique `Use` can be shown for other
@@ -1870,25 +1837,4 @@ mod tests {
assert_eq!(uses[v4], ValueUseState::Once);
assert_eq!(uses[v5], ValueUseState::Once);
}
-
- #[test]
- fn results_used_twice_but_not_operands() {
- let mut func = Function::new();
- let block0 = func.dfg.make_block();
- let mut pos = FuncCursor::new(&mut func);
- pos.insert_block(block0);
- let v1 = pos.ins().iconst(types::I64, 0);
- let v2 = pos.ins().iconst(types::I64, 1);
- let v3 = pos.ins().iconcat(v1, v2);
- let (v4, v5) = pos.ins().isplit(v3);
- pos.ins().return_(&[v4, v4]);
- let func = pos.func;
-
- let uses = super::compute_use_states(&func, None);
- assert_eq!(uses[v1], ValueUseState::Once);
- assert_eq!(uses[v2], ValueUseState::Once);
- assert_eq!(uses[v3], ValueUseState::Once);
- assert_eq!(uses[v4], ValueUseState::Multiple);
- assert_eq!(uses[v5], ValueUseState::Unused);
- }
}
diff --git a/cranelift/filetests/filetests/isa/x64/uadd_overflow_brif.clif b/cranelift/filetests/filetests/isa/x64/uadd_overflow_brif.clif
index 491213d2d1..ba59e25065 100644
--- a/cranelift/filetests/filetests/isa/x64/uadd_overflow_brif.clif
+++ b/cranelift/filetests/filetests/isa/x64/uadd_overflow_brif.clif
@@ -322,3 +322,59 @@ block3:
; movq %rbp, %rsp
; popq %rbp
; retq
+
+function %no_load_sinking(i64) -> i8 {
+block0(v0: i64):
+ v1 = load.i64 v0
+ v2, v3 = uadd_overflow v0, v1
+ brif v3, block1(v3), block2
+
+block1(v4: i8):
+ return v4
+
+block2:
+ v5 = iconst.i8 42
+ return v5
+}
+
+; VCode:
+; pushq %rbp
+; movq %rsp, %rbp
+; block0:
+; movq (%rdi), %rsi
+; movq %rdi, %r8
+; addq %rsi, %r8
+; setb %al
+; addq %rsi, %rdi
+; jb label2; j label1
+; block1:
+; movl $0x2a, %eax
+; movq %rbp, %rsp
+; popq %rbp
+; retq
+; block2:
+; movq %rbp, %rsp
+; popq %rbp
+; retq
+;
+; Disassembled:
+; block0: ; offset 0x0
+; pushq %rbp
+; movq %rsp, %rbp
+; block1: ; offset 0x4
+; movq (%rdi), %rsi ; trap: heap_oob
+; movq %rdi, %r8
+; addq %rsi, %r8
+; setb %al
+; addq %rsi, %rdi
+; jb 0x23
+; block2: ; offset 0x19
+; movl $0x2a, %eax
+; movq %rbp, %rsp
+; popq %rbp
+; retq
+; block3: ; offset 0x23
+; movq %rbp, %rsp
+; popq %rbp
+; retq
+