From 432978e750b8fd7c1922687f9f2ad983dd540242 Mon Sep 17 00:00:00 2001 From: Mahmood Yassin Date: Mon, 24 Nov 2025 13:36:28 +0200 Subject: [PATCH 1/2] Support pointer argument of builtin functions --- clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp | 27 ++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp index 14b76bb8b06a..9b8056415d8d 100644 --- a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp @@ -498,7 +498,12 @@ decodeFixedType(ArrayRef &infos, return cir::VectorType::get(context, elementType, numElements); } case IITDescriptor::Pointer: - llvm_unreachable("NYI: IITDescriptor::Pointer"); + case IITDescriptor::Pointer: { + mlir::Type pointee = {}; + auto addrSpace = + static_cast(descriptor.Pointer_AddressSpace); + return cir::PointerType::get(context, pointee, addrSpace); + } case IITDescriptor::Struct: llvm_unreachable("NYI: IITDescriptor::Struct"); case IITDescriptor::Argument: @@ -2803,8 +2808,24 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID, mlir::Type correctedExpectedTy = getIntrinsicArgumentTypeFromAST(expectedTy, E, i, &getMLIRContext()); - if (argType != correctedExpectedTy) - llvm_unreachable("NYI"); + if (argType != correctedExpectedTy) { + if (isa(argType)) { + auto ptrType = mlir::cast(argType); + auto expectedPtrType = mlir::cast(expectedTy); + if (ptrType.getPointee() != expectedPtrType.getPointee()) { + if (expectedPtrType.getAddrSpace() != ptrType.getAddrSpace()) { + auto newPtrType = + cir::PointerType::get(&getMLIRContext(), ptrType.getPointee(), + expectedPtrType.getAddrSpace()); + argValue = builder.createAddrSpaceCast(argValue, newPtrType); + } + } else { + llvm_unreachable("NYI"); + } + } else { + llvm_unreachable("NYI"); + } + } args.push_back(argValue); } From a7311668a8a221f7af32ac4a3d40d9e8e473eda3 Mon Sep 17 00:00:00 2001 From: Mahmood Yassin Date: Tue, 25 Nov 2025 15:00:37 +0200 Subject: [PATCH 2/2] Code review fixes --- clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp | 46 ++++++++++++++----------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp index 9b8056415d8d..d28b4abf7231 100644 --- a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp @@ -497,12 +497,11 @@ decodeFixedType(ArrayRef &infos, unsigned numElements = descriptor.Vector_Width.getFixedValue(); return cir::VectorType::get(context, elementType, numElements); } - case IITDescriptor::Pointer: case IITDescriptor::Pointer: { mlir::Type pointee = {}; auto addrSpace = static_cast(descriptor.Pointer_AddressSpace); - return cir::PointerType::get(context, pointee, addrSpace); + return cir::PointerType::get(pointee, addrSpace); } case IITDescriptor::Struct: llvm_unreachable("NYI: IITDescriptor::Struct"); @@ -569,6 +568,29 @@ static mlir::Type getIntrinsicArgumentTypeFromAST(mlir::Type iitType, return iitType; } +// Ensures a pointer argument matches the expected CIR pointer type, +// emitting an addrspacecast for address-space mismatches only. +static mlir::Value getCorrectedPtr(mlir::Value argValue, mlir::Type expectedTy, + CIRGenBuilderTy &builder) { + mlir::Type argType = argValue.getType(); + if (isa(argType)) { + auto ptrType = mlir::cast(argType); + auto expectedPtrType = mlir::cast(expectedTy); + if (ptrType.getPointee() != expectedPtrType.getPointee()) { + if (expectedPtrType.getAddrSpace() != ptrType.getAddrSpace()) { + auto newPtrType = cir::PointerType::get(ptrType.getPointee(), + expectedPtrType.getAddrSpace()); + return builder.createAddrSpaceCast(argValue, newPtrType); + } + } else { + llvm_unreachable("NYI"); + } + } else { + llvm_unreachable("NYI"); + } + return argValue; +} + static cir::FuncType getIntrinsicType(mlir::MLIRContext *context, llvm::Intrinsic::ID id) { using namespace llvm::Intrinsic; @@ -2808,24 +2830,8 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID, mlir::Type correctedExpectedTy = getIntrinsicArgumentTypeFromAST(expectedTy, E, i, &getMLIRContext()); - if (argType != correctedExpectedTy) { - if (isa(argType)) { - auto ptrType = mlir::cast(argType); - auto expectedPtrType = mlir::cast(expectedTy); - if (ptrType.getPointee() != expectedPtrType.getPointee()) { - if (expectedPtrType.getAddrSpace() != ptrType.getAddrSpace()) { - auto newPtrType = - cir::PointerType::get(&getMLIRContext(), ptrType.getPointee(), - expectedPtrType.getAddrSpace()); - argValue = builder.createAddrSpaceCast(argValue, newPtrType); - } - } else { - llvm_unreachable("NYI"); - } - } else { - llvm_unreachable("NYI"); - } - } + if (argType != correctedExpectedTy) + argValue = getCorrectedPtr(argValue, expectedTy, builder); args.push_back(argValue); }