From 8dede2eb778608f73733beb3e13fdaf9ae76ab21 Mon Sep 17 00:00:00 2001 From: 7mile Date: Tue, 21 May 2024 03:12:27 +0800 Subject: [PATCH] [CIR][CIRGen][LowerToLLVM] Add address space attribute for pointer type (#606) This is the prelude of address space support. Linked issue: #418 . - Add the attribute and implement asm format & type conversion. - Make ops like `cir.global` and `cir.get_global` aware of address space, and solve the latter flag. - Relax the restriction of default alloca address space. Then we can use correct address spaces for languages like OpenCL in future. --- .../include/clang/CIR/Dialect/IR/CIRTypes.td | 20 +++++++++++++++++-- clang/lib/CIR/CodeGen/CIRGenTypes.cpp | 12 +++++------ .../CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 3 ++- clang/test/CIR/CodeGen/address-space.c | 11 ++++++++++ clang/test/CIR/IR/address-space.cir | 11 ++++++++++ clang/test/CIR/Lowering/address-space.cir | 20 +++++++++++++++++++ 6 files changed, 68 insertions(+), 9 deletions(-) create mode 100644 clang/test/CIR/CodeGen/address-space.c create mode 100644 clang/test/CIR/IR/address-space.cir create mode 100644 clang/test/CIR/Lowering/address-space.cir diff --git a/clang/include/clang/CIR/Dialect/IR/CIRTypes.td b/clang/include/clang/CIR/Dialect/IR/CIRTypes.td index 2babdee1d289..ca7ed7730220 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIRTypes.td +++ b/clang/include/clang/CIR/Dialect/IR/CIRTypes.td @@ -208,9 +208,25 @@ def CIR_PointerType : CIR_Type<"Pointer", "ptr", `CIR.ptr` is a type returned by any op generating a pointer in C++. }]; - let parameters = (ins "mlir::Type":$pointee); + let parameters = (ins "mlir::Type":$pointee, + DefaultValuedParameter<"unsigned", "0">:$addrSpace); - let assemblyFormat = "`<` $pointee `>`"; + let builders = [ + TypeBuilderWithInferredContext<(ins + "mlir::Type":$pointee, CArg<"unsigned", "0">:$addrSpace), [{ + return Base::get(pointee.getContext(), pointee, addrSpace); + }]>, + TypeBuilder<(ins + "mlir::Type":$pointee, CArg<"unsigned", "0">:$addrSpace), [{ + return Base::get($_ctxt, pointee, addrSpace); + }]>, + ]; + + let assemblyFormat = [{ + `<` $pointee ( `,` `addrspace` `(` $addrSpace^ `)` )? `>` + }]; + + let skipDefaultBuilders = 1; let extraClassDeclaration = [{ bool isVoidPtr() const { diff --git a/clang/lib/CIR/CodeGen/CIRGenTypes.cpp b/clang/lib/CIR/CodeGen/CIRGenTypes.cpp index 6b3b0c2268fa..fd398c1128fe 100644 --- a/clang/lib/CIR/CodeGen/CIRGenTypes.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenTypes.cpp @@ -598,9 +598,9 @@ mlir::Type CIRGenTypes::ConvertType(QualType T) { const ReferenceType *RTy = cast(Ty); QualType ETy = RTy->getPointeeType(); auto PointeeType = convertTypeForMem(ETy); - // TODO(cir): use Context.getTargetAddressSpace(ETy) on pointer - ResultType = - ::mlir::cir::PointerType::get(Builder.getContext(), PointeeType); + ResultType = ::mlir::cir::PointerType::get( + Builder.getContext(), PointeeType, + Context.getTargetAddressSpace(ETy.getAddressSpace())); assert(ResultType && "Cannot get pointer type?"); break; } @@ -615,9 +615,9 @@ mlir::Type CIRGenTypes::ConvertType(QualType T) { // if (PointeeType->isVoidTy()) // PointeeType = Builder.getI8Type(); - // FIXME: add address specifier to cir::PointerType? - ResultType = - ::mlir::cir::PointerType::get(Builder.getContext(), PointeeType); + ResultType = ::mlir::cir::PointerType::get( + Builder.getContext(), PointeeType, + Context.getTargetAddressSpace(ETy.getAddressSpace())); assert(ResultType && "Cannot get pointer type?"); break; } diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp index debd9dd6dedb..ddd5332e99e7 100644 --- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp +++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp @@ -3184,7 +3184,8 @@ void prepareTypeConverter(mlir::LLVMTypeConverter &converter, mlir::DataLayout &dataLayout) { converter.addConversion([&](mlir::cir::PointerType type) -> mlir::Type { // Drop pointee type since LLVM dialect only allows opaque pointers. - return mlir::LLVM::LLVMPointerType::get(type.getContext()); + return mlir::LLVM::LLVMPointerType::get(type.getContext(), + type.getAddrSpace()); }); converter.addConversion([&](mlir::cir::ArrayType type) -> mlir::Type { auto ty = converter.convertType(type.getEltType()); diff --git a/clang/test/CIR/CodeGen/address-space.c b/clang/test/CIR/CodeGen/address-space.c new file mode 100644 index 000000000000..047aa25bbcc7 --- /dev/null +++ b/clang/test/CIR/CodeGen/address-space.c @@ -0,0 +1,11 @@ +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir +// RUN: FileCheck --input-file=%t.cir %s -check-prefix=CIR +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -S -emit-llvm %s -o %t.ll +// RUN: FileCheck --input-file=%t.ll %s -check-prefix=LLVM +// XFAIL: * + +// CIR: cir.func {{@.*foo.*}}(%arg0: !cir.ptr +// LLVM: define void @foo(ptr addrspace(1) %0) +void foo(int __attribute__((address_space(1))) *arg) { + return; +} diff --git a/clang/test/CIR/IR/address-space.cir b/clang/test/CIR/IR/address-space.cir new file mode 100644 index 000000000000..dde39bdd4d73 --- /dev/null +++ b/clang/test/CIR/IR/address-space.cir @@ -0,0 +1,11 @@ +// RUN: cir-opt %s -o %t.cir +// RUN: FileCheck --input-file=%t.cir %s + +!s32i = !cir.int + +module { + // CHECK: @test_addrspace_assembly_format(%arg0: !cir.ptr) + cir.func @test_addrspace_assembly_format(%arg0: !cir.ptr) { + cir.return + } +} diff --git a/clang/test/CIR/Lowering/address-space.cir b/clang/test/CIR/Lowering/address-space.cir new file mode 100644 index 000000000000..c7d5a84829c4 --- /dev/null +++ b/clang/test/CIR/Lowering/address-space.cir @@ -0,0 +1,20 @@ +// RUN: cir-translate %s -cir-to-llvmir -o %t.ll +// RUN: FileCheck --input-file=%t.ll %s -check-prefix=LLVM + +!s32i = !cir.int + +module { + // LLVM: define void @foo(ptr %0) + cir.func @foo(%arg0: !cir.ptr) { + // LLVM-NEXT: alloca ptr, + %0 = cir.alloca !cir.ptr, !cir.ptr>, ["arg", init] {alignment = 8 : i64} + cir.return + } + + // LLVM: define void @bar(ptr addrspace(1) %0) + cir.func @bar(%arg0: !cir.ptr) { + // LLVM-NEXT: alloca ptr addrspace(1) + %0 = cir.alloca !cir.ptr, !cir.ptr>, ["arg", init] {alignment = 8 : i64} + cir.return + } +}