diff --git a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp index 61cb04828e272..f4582fdf0ccc1 100644 --- a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp @@ -1180,6 +1180,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 different 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(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(argValue.getType()); @@ -3056,14 +3077,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; +}