Skip to content

Commit 6f1be95

Browse files
committed
[CIR][Dialect] Add offload_* cases in address space attribute
fix dirty include
1 parent a1c3cc5 commit 6f1be95

File tree

10 files changed

+84
-20
lines changed

10 files changed

+84
-20
lines changed

clang/include/clang/CIR/Dialect/IR/CIRAttrs.td

+1
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,7 @@ def AddressSpaceAttr : CIR_Attr<"AddressSpace", "addrspace"> {
707707
let extraClassDeclaration = [{
708708
static constexpr char kTargetKeyword[] = "}]#targetASCase.symbol#[{";
709709
static constexpr int32_t kFirstTargetASValue = }]#targetASCase.value#[{;
710+
using MapTy = unsigned[kFirstTargetASValue];
710711

711712
bool isLang() const;
712713
bool isTarget() const;

clang/include/clang/CIR/MissingFeatures.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,6 @@ struct MissingFeatures {
161161
static bool constantFoldsToSimpleInteger() { return false; }
162162
static bool checkFunctionCallABI() { return false; }
163163
static bool zeroInitializer() { return false; }
164-
static bool targetLoweringInfoAddressSpaceMap() { return false; }
165164
static bool targetCodeGenInfoIsProtoCallVariadic() { return false; }
166165
static bool targetCodeGenInfoGetNullPointer() { return false; }
167166
static bool operandBundles() { return false; }
@@ -277,6 +276,10 @@ struct MissingFeatures {
277276
static bool returnValueDominatingStoreOptmiization() { return false; }
278277
// Globals (vars and functions) may have attributes that are target depedent.
279278
static bool setTargetAttributes() { return false; }
279+
280+
// CIR modules parsed from text form may not carry the triple or data layout
281+
// specs. We should make it always present.
282+
static bool makeTripleAlwaysPresent() { return false; }
280283
};
281284

282285
} // namespace cir

clang/lib/CIR/Dialect/IR/CIRAttrs.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -565,10 +565,16 @@ AddressSpaceAttr::getValueFromLangAS(clang::LangAS langAS) {
565565
// Default address space should be encoded as a null attribute.
566566
return std::nullopt;
567567
case LangAS::opencl_global:
568+
return Kind::offload_global;
568569
case LangAS::opencl_local:
570+
return Kind::offload_local;
569571
case LangAS::opencl_constant:
572+
return Kind::offload_constant;
570573
case LangAS::opencl_private:
574+
return Kind::offload_private;
571575
case LangAS::opencl_generic:
576+
return Kind::offload_generic;
577+
572578
case LangAS::opencl_global_device:
573579
case LangAS::opencl_global_host:
574580
case LangAS::cuda_device:

clang/lib/CIR/Dialect/Transforms/TargetLowering/TargetInfo.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,10 @@ TargetLoweringInfo::TargetLoweringInfo(std::unique_ptr<ABIInfo> Info)
88

99
TargetLoweringInfo::~TargetLoweringInfo() = default;
1010

11+
AddressSpaceAttr::MapTy const &TargetLoweringInfo::getCIRAddrSpaceMap() const {
12+
static AddressSpaceAttr::MapTy defaultAddrSpaceMap = {0};
13+
return defaultAddrSpaceMap;
14+
}
15+
1116
} // namespace cir
1217
} // namespace mlir

clang/lib/CIR/Dialect/Transforms/TargetLowering/TargetLoweringInfo.h

+3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#include "ABIInfo.h"
1919
#include <memory>
2020

21+
#include "clang/CIR/Dialect/IR/CIRAttrs.h"
22+
2123
namespace mlir {
2224
namespace cir {
2325

@@ -30,6 +32,7 @@ class TargetLoweringInfo {
3032
virtual ~TargetLoweringInfo();
3133

3234
const ABIInfo &getABIInfo() const { return *Info; }
35+
virtual AddressSpaceAttr::MapTy const &getCIRAddrSpaceMap() const;
3336
};
3437

3538
} // namespace cir

clang/lib/CIR/Dialect/Transforms/TargetLowering/Targets/SPIR.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "TargetInfo.h"
1313
#include "TargetLoweringInfo.h"
1414
#include "clang/CIR/ABIArgInfo.h"
15+
#include "clang/CIR/Dialect/IR/CIRTypes.h"
1516
#include "clang/CIR/MissingFeatures.h"
1617
#include "llvm/Support/ErrorHandling.h"
1718

@@ -37,10 +38,27 @@ class SPIRVABIInfo : public ABIInfo {
3738
}
3839
};
3940

41+
//===----------------------------------------------------------------------===//
42+
// SPIR-V other target-specific information
43+
//===----------------------------------------------------------------------===//
44+
45+
static AddressSpaceAttr::MapTy SPIRVAddrSpaceMap = {
46+
0, // None
47+
0, // offload_private
48+
3, // offload_local
49+
1, // offload_global
50+
2, // offload_constant
51+
4, // offload_generic
52+
};
53+
4054
class SPIRVTargetLoweringInfo : public TargetLoweringInfo {
4155
public:
4256
SPIRVTargetLoweringInfo(LowerTypes &LT)
4357
: TargetLoweringInfo(std::make_unique<SPIRVABIInfo>(LT)) {}
58+
59+
AddressSpaceAttr::MapTy const &getCIRAddrSpaceMap() const override {
60+
return SPIRVAddrSpaceMap;
61+
}
4462
};
4563

4664
} // namespace

clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp

+32-10
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@
6868
#include <optional>
6969
#include <set>
7070

71+
#include "LowerModule.h"
72+
7173
using namespace cir;
7274
using namespace llvm;
7375

@@ -3583,24 +3585,44 @@ void populateCIRToLLVMConversionPatterns(mlir::RewritePatternSet &patterns,
35833585
}
35843586

35853587
namespace {
3588+
3589+
using IRASMap = mlir::cir::AddressSpaceAttr::MapTy;
3590+
3591+
/// Returns the CIR address space map when available. Returns nullptr if the
3592+
/// triple is not present in the module.
3593+
IRASMap const *getIRAddrSpaceMap(mlir::ModuleOp module) {
3594+
// If the triple is not present, e.g. CIR modules parsed from text, we
3595+
// cannot init LowerModule properly.
3596+
assert(!::cir::MissingFeatures::makeTripleAlwaysPresent());
3597+
// Here we will use a default AS map to ignore all AS stuff.
3598+
if (!module->hasAttr("cir.triple"))
3599+
return nullptr;
3600+
mlir::PatternRewriter rewriter{module->getContext()};
3601+
auto lowerModule = mlir::cir::createLowerModule(module, rewriter);
3602+
// All CIR AS maps have static lifetime, it's safe to ref it
3603+
return &lowerModule.getTargetLoweringInfo().getCIRAddrSpaceMap();
3604+
};
3605+
35863606
void prepareTypeConverter(mlir::LLVMTypeConverter &converter,
3587-
mlir::DataLayout &dataLayout) {
3588-
converter.addConversion([&](mlir::cir::PointerType type) -> mlir::Type {
3607+
mlir::DataLayout &dataLayout,
3608+
IRASMap const *cirASMap) {
3609+
converter.addConversion([&, cirASMap](
3610+
mlir::cir::PointerType type) -> mlir::Type {
35893611
// Drop pointee type since LLVM dialect only allows opaque pointers.
35903612

35913613
auto addrSpace =
35923614
mlir::cast_if_present<mlir::cir::AddressSpaceAttr>(type.getAddrSpace());
3593-
// null addrspace attribute indicates the default addrspace
3615+
// Null addrspace attribute indicates the default addrspace
35943616
if (!addrSpace)
35953617
return mlir::LLVM::LLVMPointerType::get(type.getContext());
35963618

3597-
// TODO(cir): Query the target-specific address space map to lower other ASs
3598-
// like `opencl_private`.
3599-
assert(!MissingFeatures::targetLoweringInfoAddressSpaceMap());
3600-
assert(addrSpace.isTarget() && "NYI");
3619+
assert(cirASMap && "CIR AS map is not available");
3620+
// Pass through target addrspace and map CIR addrspace to LLVM addrspace.
3621+
unsigned targetAS = addrSpace.isTarget()
3622+
? addrSpace.getTargetValue()
3623+
: (*cirASMap)[addrSpace.getValue()];
36013624

3602-
return mlir::LLVM::LLVMPointerType::get(type.getContext(),
3603-
addrSpace.getTargetValue());
3625+
return mlir::LLVM::LLVMPointerType::get(type.getContext(), targetAS);
36043626
});
36053627
converter.addConversion([&](mlir::cir::DataMemberType type) -> mlir::Type {
36063628
return mlir::IntegerType::get(type.getContext(),
@@ -3825,7 +3847,7 @@ void ConvertCIRToLLVMPass::runOnOperation() {
38253847
auto module = getOperation();
38263848
mlir::DataLayout dataLayout(module);
38273849
mlir::LLVMTypeConverter converter(&getContext());
3828-
prepareTypeConverter(converter, dataLayout);
3850+
prepareTypeConverter(converter, dataLayout, getIRAddrSpaceMap(module));
38293851

38303852
mlir::RewritePatternSet patterns(&getContext());
38313853

clang/test/CIR/CodeGen/OpenCL/addrspace-alloca.cl

+4-6
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,23 @@
33
// RUN: %clang_cc1 -cl-std=CL3.0 -O0 -fclangir -emit-llvm -triple spirv64-unknown-unknown %s -o %t.ll
44
// RUN: FileCheck --input-file=%t.ll %s --check-prefix=LLVM
55

6-
// Lowering of language-specific AS not supported
7-
// XFAIL: *
86

9-
// CIR: cir.func @func(%arg0: !cir.ptr<!s32i, addrspace(target<3>)>
7+
// CIR: cir.func @func(%arg0: !cir.ptr<!s32i, addrspace(offload_local)>
108
// LLVM: @func(ptr addrspace(3)
119
kernel void func(local int *p) {
12-
// CIR-NEXT: %[[#ALLOCA_P:]] = cir.alloca !cir.ptr<!s32i, addrspace(target<3>)>, !cir.ptr<!cir.ptr<!s32i, addrspace(target<3>)>>, ["p", init] {alignment = 8 : i64}
10+
// CIR-NEXT: %[[#ALLOCA_P:]] = cir.alloca !cir.ptr<!s32i, addrspace(offload_local)>, !cir.ptr<!cir.ptr<!s32i, addrspace(offload_local)>>, ["p", init] {alignment = 8 : i64}
1311
// LLVM-NEXT: %[[#ALLOCA_P:]] = alloca ptr addrspace(3), i64 1, align 8
1412

1513
int x;
1614
// CIR-NEXT: %[[#ALLOCA_X:]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["x"] {alignment = 4 : i64}
1715
// LLVM-NEXT: %[[#ALLOCA_X:]] = alloca i32, i64 1, align 4
1816

1917
global char *b;
20-
// CIR-NEXT: %[[#ALLOCA_B:]] = cir.alloca !cir.ptr<!s8i, addrspace(target<1>)>, !cir.ptr<!cir.ptr<!s8i, addrspace(target<1>)>>, ["b"] {alignment = 8 : i64}
18+
// CIR-NEXT: %[[#ALLOCA_B:]] = cir.alloca !cir.ptr<!s8i, addrspace(offload_global)>, !cir.ptr<!cir.ptr<!s8i, addrspace(offload_global)>>, ["b"] {alignment = 8 : i64}
2119
// LLVM-NEXT: %[[#ALLOCA_B:]] = alloca ptr addrspace(1), i64 1, align 8
2220

2321
// Store of the argument `p`
24-
// CIR-NEXT: cir.store %arg0, %[[#ALLOCA_P]] : !cir.ptr<!s32i, addrspace(target<3>)>, !cir.ptr<!cir.ptr<!s32i, addrspace(target<3>)>>
22+
// CIR-NEXT: cir.store %arg0, %[[#ALLOCA_P]] : !cir.ptr<!s32i, addrspace(offload_local)>, !cir.ptr<!cir.ptr<!s32i, addrspace(offload_local)>>
2523
// LLVM-NEXT: store ptr addrspace(3) %{{[0-9]+}}, ptr %[[#ALLOCA_P]], align 8
2624

2725
return;

clang/test/CIR/CodeGen/OpenCL/spirv-target.cl

-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
// RUN: %clang_cc1 -cl-std=CL3.0 -fclangir -emit-llvm -triple spirv64-unknown-unknown %s -o %t_64.ll
55
// RUN: FileCheck --input-file=%t_64.ll %s --check-prefix=LLVM-SPIRV64
66

7-
// Lowering of language-specific AS not supported
8-
// XFAIL: *
97

108
// CIR-SPIRV64: cir.triple = "spirv64-unknown-unknown"
119
// LLVM-SPIRV64: target triple = "spirv64-unknown-unknown"

clang/test/CIR/Lowering/address-space.cir

+11-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33

44
!s32i = !cir.int<s, 32>
55

6-
module {
6+
module attributes {
7+
cir.triple = "spirv64-unknown-unknown",
8+
llvm.data_layout = "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024-G1"
9+
} {
710
// LLVM: define void @foo(ptr %0)
811
cir.func @foo(%arg0: !cir.ptr<!s32i>) {
912
// LLVM-NEXT: alloca ptr,
@@ -24,4 +27,11 @@ module {
2427
%0 = cir.alloca !cir.ptr<!s32i, addrspace(target<0>)>, !cir.ptr<!cir.ptr<!s32i, addrspace(target<0>)>>, ["arg", init] {alignment = 8 : i64}
2528
cir.return
2629
}
30+
31+
// LLVM: define void @qux(ptr addrspace(4) %0)
32+
cir.func @qux(%arg0: !cir.ptr<!s32i, addrspace(offload_generic)>) {
33+
// LLVM-NEXT: alloca ptr addrspace(4),
34+
%0 = cir.alloca !cir.ptr<!s32i, addrspace(offload_generic)>, !cir.ptr<!cir.ptr<!s32i, addrspace(offload_generic)>>, ["arg", init] {alignment = 8 : i64}
35+
cir.return
36+
}
2737
}

0 commit comments

Comments
 (0)