diff --git a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp index 14b76bb8b06a..d28b4abf7231 100644 --- a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp @@ -497,8 +497,12 @@ decodeFixedType(ArrayRef &infos, unsigned numElements = descriptor.Vector_Width.getFixedValue(); 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(pointee, addrSpace); + } case IITDescriptor::Struct: llvm_unreachable("NYI: IITDescriptor::Struct"); case IITDescriptor::Argument: @@ -564,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; @@ -2804,7 +2831,7 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID, getIntrinsicArgumentTypeFromAST(expectedTy, E, i, &getMLIRContext()); if (argType != correctedExpectedTy) - llvm_unreachable("NYI"); + argValue = getCorrectedPtr(argValue, expectedTy, builder); args.push_back(argValue); }