From bf4d0e92125c4d8b666703f36506ea1f6328026c Mon Sep 17 00:00:00 2001 From: Nick Fitzgerald Date: Mon, 30 Jan 2023 21:25:14 -0800 Subject: [PATCH] Cranelift: Fix `select` condition harvesting (#5662) Souper requires an `i1` condition value, we don't and will implicitly check against 0. We were truncating conditions but should actually be doing the comparison against `0`. --- cranelift/codegen/src/souper_harvest.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/cranelift/codegen/src/souper_harvest.rs b/cranelift/codegen/src/souper_harvest.rs index a690fa7262..d818eee9f9 100644 --- a/cranelift/codegen/src/souper_harvest.rs +++ b/cranelift/codegen/src/souper_harvest.rs @@ -387,7 +387,8 @@ fn harvest_candidate_lhs( let a = arg(allocs, 0); // While Cranelift allows any width condition for - // `select`, Souper requires an `i1`. + // `select` and checks it against `0`, Souper requires + // an `i1`. So insert a `ne %x, 0` as needed. let a = match a { ast::Operand::Value(id) => match lhs.get_value(id).r#type { Some(ast::Type { width: 1 }) => a, @@ -395,7 +396,14 @@ fn harvest_candidate_lhs( .assignment( None, Some(ast::Type { width: 1 }), - ast::Instruction::Trunc { a }, + ast::Instruction::Ne { + a, + b: ast::Constant { + value: 0, + r#type: None, + } + .into(), + }, vec![], ) .into(),