sunfishcode / llvm2cranelift

LLVM IR to Cranelift IR translator
Apache License 2.0
36 stars 4 forks source link

fix crash when encountering a comparison with 'null' or non inst values (e.g. llvm::ConstExpressions) #10

Closed undingen closed 4 years ago

undingen commented 5 years ago

before this fix

define i32 @foo(i8*) {
  %2 = icmp ne i8* %0, null
  %3 = zext i1 %2 to i32
  ret i32 %3
 }

either crashed or just inserted a random value (e.g 33 in my case):

; foo
function u0:0(i64) -> i32 system_v {
ebb0(v0: i64):
    v1 = icmp_imm ne v0, 33
    v2 = bint.i32 v1
    return v2
}
undingen commented 5 years ago

I added a commit which fixes many more cases of garbage values/crashes when encountering comparisons with non int values

The example at the bottom produces comparisons with random values. With this change we produce the correct IR:

function u0:0(i64) -> i32 system_v {
ebb0(v0: i64):
    v1 = iconst.i64 100
    v2 = icmp eq v0, v1
    v3 = bint.i32 v2
    return v3
}

in the future we could specialcase llvm::ConstExpr which produce a constant integer and directly merge it into the comparison to generate one less instruction. But for now this enough to produce correct IR.

define dso_local i32 @foo(i8* readnone) {
  %2 = icmp eq i8* %0, inttoptr (i64 100 to i8*)
  %3 = zext i1 %2 to i32
  ret i32 %3
}
sunfishcode commented 4 years ago

Nice!