Author: Steffen Larsen Date: 2026-09-07T08:53:56+02:00 New Revision: a9e273890dc99a3c2662fb14f244fa0acdb1155b
URL: https://github.com/llvm/llvm-project/commit/a9e273890dc99a3c2662fb14f244fa0acdb1155b DIFF: https://github.com/llvm/llvm-project/commit/a9e273890dc99a3c2662fb14f244fa0acdb1155b.diff LOG: [CIR] Convert i1 intrinsic results for bool-returning builtins (#220576) A target builtin with no special case is emitted through the generic intrinsic path, which returns the intrinsic's own result type. For a builtin declared to return bool that type is i1, which CIR models as !cir.int<u, 1> rather than !cir.bool. cir.if accepts only !cir.bool, so CIR builtin handling needs to convert i1 results to !cir.bool when used in this context. This is verified using __builtin_amdgcn_is_shared. --------- Signed-off-by: Steffen Holst Larsen <[email protected]> Added: clang/test/CIR/CodeGenHIP/builtin-bool-result.hip Modified: clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp Removed: ################################################################################ diff --git a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp index ab43ecc3d204a..d4c17d3c5f24f 100644 --- a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp @@ -1176,6 +1176,27 @@ static mlir::Type correctIntegerSignedness(mlir::Type iitType, QualType astType, return iitType; } +/// Helper function to correct the return type for intrinsic calls. This is +/// needed because the AST FunctionDecl may have a diff erent return type than +/// the intrinsic's IIT descriptor. For example, builtins may need their +/// signedness corrected, or a builtin may return a bool while the intrinsic +/// returns an i1. +static mlir::Type correctReturnType(mlir::Type iitType, + const FunctionDecl *funcDecl, + mlir::MLIRContext *context) { + if (!funcDecl) + return iitType; + QualType astType = funcDecl->getReturnType(); + + // Relabel the return type to cir.bool if the builtin returns a bool and + // the intrinsic returns an i1. + auto intTy = mlir::dyn_cast<cir::IntType>(iitType); + if (intTy && intTy.getWidth() == 1 && astType->isBooleanType()) + return cir::BoolType::get(context); + + return correctIntegerSignedness(iitType, astType, context); +} + static mlir::Value getCorrectedPtr(mlir::Value argValue, mlir::Type expectedTy, CIRGenBuilderTy &builder) { auto ptrType = mlir::cast<cir::PointerType>(argValue.getType()); @@ -3051,14 +3072,10 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl &gd, unsigned builtinID, args.push_back(argValue); } - // Correct return type signedness based on AST return type before creating - // the call, avoiding unnecessary casts in the IR. - mlir::Type correctedReturnType = intrinsicType.getReturnType(); - if (fd) { - correctedReturnType = - correctIntegerSignedness(intrinsicType.getReturnType(), - fd->getReturnType(), &getMLIRContext()); - } + // Correct the builtin type based on the AST function declaration's return + // type, if available. + mlir::Type correctedReturnType = + correctReturnType(intrinsicType.getReturnType(), fd, &getMLIRContext()); cir::LLVMIntrinsicCallOp intrinsicCall = cir::LLVMIntrinsicCallOp::create( builder, getLoc(e->getExprLoc()), builder.getStringAttr(name), diff --git a/clang/test/CIR/CodeGenHIP/builtin-bool-result.hip b/clang/test/CIR/CodeGenHIP/builtin-bool-result.hip new file mode 100644 index 0000000000000..28efe28e4489f --- /dev/null +++ b/clang/test/CIR/CodeGenHIP/builtin-bool-result.hip @@ -0,0 +1,28 @@ +// REQUIRES: amdgpu-registered-target +// RUN: %clang_cc1 -triple amdgcn-amd-amdhsa -fclangir -fcuda-is-device \ +// RUN: -emit-cir %s -o - | FileCheck --check-prefix=CIR %s +// RUN: %clang_cc1 -triple amdgcn-amd-amdhsa -fclangir -fcuda-is-device \ +// RUN: -emit-llvm %s -o - | FileCheck --check-prefix=LLVM %s +// +// RUN: %clang_cc1 -triple amdgcn-amd-amdhsa -fcuda-is-device -emit-llvm %s \ +// RUN: -o - | FileCheck --check-prefix=LLVM %s + +// Checks that builtins returning an i1 can be used as bool results implicitly. + +#define __device__ __attribute__((device)) + +// CIR-LABEL: cir.func {{.*}} @_Z2shPv +// CIR: cir.call_llvm_intrinsic "amdgcn.is.shared" {{.*}} -> !cir.bool +// LLVM-LABEL: @_Z2shPv +// LLVM: call i1 @llvm.amdgcn.is.shared(ptr +__device__ bool sh(void *p) { return __builtin_amdgcn_is_shared(p); } + +// CIR-LABEL: cir.func {{.*}} @_Z3usePv +// CIR: cir.call_llvm_intrinsic "amdgcn.is.shared" {{.*}} -> !cir.bool +// LLVM-LABEL: @_Z3usePv +// LLVM: call i1 @llvm.amdgcn.is.shared(ptr +__device__ int use(void *p) { + if (__builtin_amdgcn_is_shared(p)) + return 1; + return 0; +} _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
