clangir
clangir copied to clipboard
`!cir.bool` lowering differs from original clang CodeGen.
I believe the root problem here is that CIR lowers !cir.bool
in a different way than the original clang CodeGen.
In the original CodeGen, bool
glvalues are lowered to LLVM i8
values, and bool
prvalues are lowered to LLVM i1
values, as illustrated in the following example:
bool test(int x) {
bool ret = x > 42;
return ret;
}
; @test returns a bool prvalue so its return type is `i1`
define dso_local noundef zeroext i1 @test()() {
entry:
%ret = alloca i8, align 1 ; %ret is a bool glvalue so its type is `i8`
tail call void @llvm.dbg.declare(metadata ptr %ret, metadata !16, metadata !DIExpression())
store i8 1, ptr %ret, align 1
%0 = load i8, ptr %ret, align 1
%tobool = trunc i8 %0 to i1
ret i1 %tobool
}
However, in CIRGen, all !cir.bool
values are lowered to LLVM i8
values. The example above would be lowered to LLVMIR through CIR as:
; Note that the return value of @test is `i8` rather than `i1`
define i8 @test()() #0 !dbg !3 {
%1 = alloca i8, i64 1, align 1, !dbg !6
%2 = alloca i8, i64 1, align 1, !dbg !7
store i8 1, ptr %2, align 1, !dbg !7
%3 = load i8, ptr %2, align 1, !dbg !8
store i8 %3, ptr %1, align 1, !dbg !9
%4 = load i8, ptr %1, align 1, !dbg !9
ret i8 %4, !dbg !9
}
This divergence leads to the redundancy illustrated in the PR description. The result of a cir.cmp
operation is currently expected to be lowered to an i8
value although it's a prvalue. After emitting an llvm.icmp
operation, you have to insert an llvm.zext
operation to extend the i1
to i8
. Thus the redundancy.
Originally posted by @Lancern in https://github.com/llvm/clangir/issues/478#issuecomment-1958928208
Hi, I'm new to open source and llvm, and I want start contributing with this issue. To my understanding, the codegen for CIR produces the return value with extra bits, which is redundant since it requires extra casting for other operations. One thing I'm confused is the comment mentioned that the problem might be generated from LowerExpectIntrinsicPass, which is the infrastructure from llvm. Do you have any suggestions where to look at to get started? Thanks!
@wolfcomos nice, welcome! You can ignore the LowerExpectIntrinsicPass
for now, since this is related to other things. To get started I suggest:
- Use the simple C example from above comment.
- Use a debugger in clang to see how traditionally it goes from Clang AST to LLVM IR
- Look at CIRGen and try to implement similar logic where things are currently working in a different way
After discussions with @sitio-couto this will be solved by his ABI work that is work in progress, so assigning to him.
#32 had some prior discussion of this, and we're running into it again in #962