From b74b01030c6a229faf7228c5f2fac436ebd28547 Mon Sep 17 00:00:00 2001 From: seven-mile Date: Wed, 13 Mar 2024 16:29:58 +0800 Subject: [PATCH 1/6] [CIR][CIRGen] Add address space for Pointer type --- clang/include/clang/CIR/Dialect/IR/CIRTypes.td | 17 +++++++++++++++-- clang/lib/CIR/CodeGen/CIRGenTypes.cpp | 12 ++++++------ 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/clang/include/clang/CIR/Dialect/IR/CIRTypes.td b/clang/include/clang/CIR/Dialect/IR/CIRTypes.td index ee8b266ed962..9f77fd7d5ca0 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIRTypes.td +++ b/clang/include/clang/CIR/Dialect/IR/CIRTypes.td @@ -194,9 +194,22 @@ 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 157d68435571..53787563eb94 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; } From f84f187b4eb884fa47db51187edbac3cfeae4d48 Mon Sep 17 00:00:00 2001 From: seven-mile Date: Wed, 15 May 2024 16:12:28 +0800 Subject: [PATCH 2/6] [CIR][NFC] Add a simple test for address space --- clang/test/CIR/CodeGen/address-space.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 clang/test/CIR/CodeGen/address-space.c diff --git a/clang/test/CIR/CodeGen/address-space.c b/clang/test/CIR/CodeGen/address-space.c new file mode 100644 index 000000000000..2f29a8b7ca29 --- /dev/null +++ b/clang/test/CIR/CodeGen/address-space.c @@ -0,0 +1,12 @@ +// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir +// RUN: FileCheck --input-file=%t.cir %s + +// CHECK: cir.func {{@.*foo.*}}(%arg0: !cir.ptr +void foo(int __attribute__((address_space(0))) *arg) { + return; +} + +// CHECK: cir.func {{@.*bar.*}}(%arg0: !cir.ptr +void bar(int __attribute__((address_space(1))) *arg) { + return; +} From 6229950db9c46e97d81551030eb0a6e01cc11160 Mon Sep 17 00:00:00 2001 From: seven-mile Date: Wed, 15 May 2024 16:41:35 +0800 Subject: [PATCH 3/6] [CIR][LowerToLLVM] Make type converter aware of addrspace --- .../CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp | 3 ++- clang/test/CIR/Lowering/address-space.cir | 20 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 clang/test/CIR/Lowering/address-space.cir diff --git a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp index a1c2f73794af..dc04663d69b4 100644 --- a/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp +++ b/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp @@ -3178,7 +3178,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/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 + } +} From 21f49b98f78a699aa0b9321a42aba44c46cb2f4d Mon Sep 17 00:00:00 2001 From: seven-mile Date: Mon, 20 May 2024 09:56:52 +0800 Subject: [PATCH 4/6] [CIR][NFC] format max line length --- clang/include/clang/CIR/Dialect/IR/CIRTypes.td | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/clang/include/clang/CIR/Dialect/IR/CIRTypes.td b/clang/include/clang/CIR/Dialect/IR/CIRTypes.td index 9f77fd7d5ca0..181175618433 100644 --- a/clang/include/clang/CIR/Dialect/IR/CIRTypes.td +++ b/clang/include/clang/CIR/Dialect/IR/CIRTypes.td @@ -194,13 +194,16 @@ 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, DefaultValuedParameter<"unsigned", "0">:$addrSpace); + let parameters = (ins "mlir::Type":$pointee, + DefaultValuedParameter<"unsigned", "0">:$addrSpace); let builders = [ - TypeBuilderWithInferredContext<(ins "mlir::Type":$pointee, CArg<"unsigned", "0">:$addrSpace), [{ + 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), [{ + TypeBuilder<(ins + "mlir::Type":$pointee, CArg<"unsigned", "0">:$addrSpace), [{ return Base::get($_ctxt, pointee, addrSpace); }]>, ]; From 62252c3326d43824655cd258a871d9c70a56c70a Mon Sep 17 00:00:00 2001 From: seven-mile Date: Mon, 20 May 2024 09:57:21 +0800 Subject: [PATCH 5/6] [CIR][NFC] Add llvm tests --- clang/test/CIR/CodeGen/address-space.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/clang/test/CIR/CodeGen/address-space.c b/clang/test/CIR/CodeGen/address-space.c index 2f29a8b7ca29..3c9cff5b4839 100644 --- a/clang/test/CIR/CodeGen/address-space.c +++ b/clang/test/CIR/CodeGen/address-space.c @@ -1,12 +1,16 @@ // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir -// RUN: FileCheck --input-file=%t.cir %s +// 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 -// CHECK: cir.func {{@.*foo.*}}(%arg0: !cir.ptr +// CIR: cir.func {{@.*foo.*}}(%arg0: !cir.ptr +// LLVM: define void @foo(ptr %0) void foo(int __attribute__((address_space(0))) *arg) { return; } -// CHECK: cir.func {{@.*bar.*}}(%arg0: !cir.ptr +// CIR: cir.func {{@.*bar.*}}(%arg0: !cir.ptr +// LLVM: define void @bar(ptr addrspace(1) %0) void bar(int __attribute__((address_space(1))) *arg) { return; } From 3828adb94d103388d4a91aca65985f29e464888b Mon Sep 17 00:00:00 2001 From: seven-mile Date: Mon, 20 May 2024 10:07:47 +0800 Subject: [PATCH 6/6] [CIR][NFC] Separate test for asm format --- clang/test/CIR/CodeGen/address-space.c | 12 +++--------- clang/test/CIR/IR/address-space.cir | 11 +++++++++++ 2 files changed, 14 insertions(+), 9 deletions(-) create mode 100644 clang/test/CIR/IR/address-space.cir diff --git a/clang/test/CIR/CodeGen/address-space.c b/clang/test/CIR/CodeGen/address-space.c index 3c9cff5b4839..0dec31fb6227 100644 --- a/clang/test/CIR/CodeGen/address-space.c +++ b/clang/test/CIR/CodeGen/address-space.c @@ -3,14 +3,8 @@ // 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 -// CIR: cir.func {{@.*foo.*}}(%arg0: !cir.ptr -// LLVM: define void @foo(ptr %0) -void foo(int __attribute__((address_space(0))) *arg) { - return; -} - -// CIR: cir.func {{@.*bar.*}}(%arg0: !cir.ptr -// LLVM: define void @bar(ptr addrspace(1) %0) -void bar(int __attribute__((address_space(1))) *arg) { +// 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 + } +}