Skip to content

Commit 276a024

Browse files
[NFC][AMDGPU] Unify AMDGPU address space enum (#73944)
Types of AMDGPU address space were defined not only in Clang-specific class but also in LLVM header. If we unify the AMD GPU address space enumeration, then we can reuse it in Clang, Flang and LLVM.
1 parent ea2e83a commit 276a024

File tree

5 files changed

+134
-126
lines changed

5 files changed

+134
-126
lines changed

clang/lib/Basic/Targets/AMDGPU.cpp

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -37,50 +37,50 @@ static const char *const DataLayoutStringAMDGCN =
3737
"-ni:7:8";
3838

3939
const LangASMap AMDGPUTargetInfo::AMDGPUDefIsGenMap = {
40-
Generic, // Default
41-
Global, // opencl_global
42-
Local, // opencl_local
43-
Constant, // opencl_constant
44-
Private, // opencl_private
45-
Generic, // opencl_generic
46-
Global, // opencl_global_device
47-
Global, // opencl_global_host
48-
Global, // cuda_device
49-
Constant, // cuda_constant
50-
Local, // cuda_shared
51-
Global, // sycl_global
52-
Global, // sycl_global_device
53-
Global, // sycl_global_host
54-
Local, // sycl_local
55-
Private, // sycl_private
56-
Generic, // ptr32_sptr
57-
Generic, // ptr32_uptr
58-
Generic, // ptr64
59-
Generic, // hlsl_groupshared
40+
llvm::AMDGPUAS::FLAT_ADDRESS, // Default
41+
llvm::AMDGPUAS::GLOBAL_ADDRESS, // opencl_global
42+
llvm::AMDGPUAS::LOCAL_ADDRESS, // opencl_local
43+
llvm::AMDGPUAS::CONSTANT_ADDRESS, // opencl_constant
44+
llvm::AMDGPUAS::PRIVATE_ADDRESS, // opencl_private
45+
llvm::AMDGPUAS::FLAT_ADDRESS, // opencl_generic
46+
llvm::AMDGPUAS::GLOBAL_ADDRESS, // opencl_global_device
47+
llvm::AMDGPUAS::GLOBAL_ADDRESS, // opencl_global_host
48+
llvm::AMDGPUAS::GLOBAL_ADDRESS, // cuda_device
49+
llvm::AMDGPUAS::CONSTANT_ADDRESS, // cuda_constant
50+
llvm::AMDGPUAS::LOCAL_ADDRESS, // cuda_shared
51+
llvm::AMDGPUAS::GLOBAL_ADDRESS, // sycl_global
52+
llvm::AMDGPUAS::GLOBAL_ADDRESS, // sycl_global_device
53+
llvm::AMDGPUAS::GLOBAL_ADDRESS, // sycl_global_host
54+
llvm::AMDGPUAS::LOCAL_ADDRESS, // sycl_local
55+
llvm::AMDGPUAS::PRIVATE_ADDRESS, // sycl_private
56+
llvm::AMDGPUAS::FLAT_ADDRESS, // ptr32_sptr
57+
llvm::AMDGPUAS::FLAT_ADDRESS, // ptr32_uptr
58+
llvm::AMDGPUAS::FLAT_ADDRESS, // ptr64
59+
llvm::AMDGPUAS::FLAT_ADDRESS, // hlsl_groupshared
6060
};
6161

6262
const LangASMap AMDGPUTargetInfo::AMDGPUDefIsPrivMap = {
63-
Private, // Default
64-
Global, // opencl_global
65-
Local, // opencl_local
66-
Constant, // opencl_constant
67-
Private, // opencl_private
68-
Generic, // opencl_generic
69-
Global, // opencl_global_device
70-
Global, // opencl_global_host
71-
Global, // cuda_device
72-
Constant, // cuda_constant
73-
Local, // cuda_shared
63+
llvm::AMDGPUAS::PRIVATE_ADDRESS, // Default
64+
llvm::AMDGPUAS::GLOBAL_ADDRESS, // opencl_global
65+
llvm::AMDGPUAS::LOCAL_ADDRESS, // opencl_local
66+
llvm::AMDGPUAS::CONSTANT_ADDRESS, // opencl_constant
67+
llvm::AMDGPUAS::PRIVATE_ADDRESS, // opencl_private
68+
llvm::AMDGPUAS::FLAT_ADDRESS, // opencl_generic
69+
llvm::AMDGPUAS::GLOBAL_ADDRESS, // opencl_global_device
70+
llvm::AMDGPUAS::GLOBAL_ADDRESS, // opencl_global_host
71+
llvm::AMDGPUAS::GLOBAL_ADDRESS, // cuda_device
72+
llvm::AMDGPUAS::CONSTANT_ADDRESS, // cuda_constant
73+
llvm::AMDGPUAS::LOCAL_ADDRESS, // cuda_shared
7474
// SYCL address space values for this map are dummy
75-
Generic, // sycl_global
76-
Generic, // sycl_global_device
77-
Generic, // sycl_global_host
78-
Generic, // sycl_local
79-
Generic, // sycl_private
80-
Generic, // ptr32_sptr
81-
Generic, // ptr32_uptr
82-
Generic, // ptr64
83-
Generic, // hlsl_groupshared
75+
llvm::AMDGPUAS::FLAT_ADDRESS, // sycl_global
76+
llvm::AMDGPUAS::FLAT_ADDRESS, // sycl_global_device
77+
llvm::AMDGPUAS::FLAT_ADDRESS, // sycl_global_host
78+
llvm::AMDGPUAS::FLAT_ADDRESS, // sycl_local
79+
llvm::AMDGPUAS::FLAT_ADDRESS, // sycl_private
80+
llvm::AMDGPUAS::FLAT_ADDRESS, // ptr32_sptr
81+
llvm::AMDGPUAS::FLAT_ADDRESS, // ptr32_uptr
82+
llvm::AMDGPUAS::FLAT_ADDRESS, // ptr64
83+
llvm::AMDGPUAS::FLAT_ADDRESS, // hlsl_groupshared
8484

8585
};
8686
} // namespace targets

clang/lib/Basic/Targets/AMDGPU.h

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "clang/Basic/TargetInfo.h"
1818
#include "clang/Basic/TargetOptions.h"
1919
#include "llvm/ADT/StringSet.h"
20+
#include "llvm/Support/AMDGPUAddrSpace.h"
2021
#include "llvm/Support/Compiler.h"
2122
#include "llvm/TargetParser/TargetParser.h"
2223
#include "llvm/TargetParser/Triple.h"
@@ -29,13 +30,6 @@ class LLVM_LIBRARY_VISIBILITY AMDGPUTargetInfo final : public TargetInfo {
2930

3031
static const char *const GCCRegNames[];
3132

32-
enum AddrSpace {
33-
Generic = 0,
34-
Global = 1,
35-
Local = 3,
36-
Constant = 4,
37-
Private = 5
38-
};
3933
static const LangASMap AMDGPUDefIsGenMap;
4034
static const LangASMap AMDGPUDefIsPrivMap;
4135

@@ -106,7 +100,8 @@ class LLVM_LIBRARY_VISIBILITY AMDGPUTargetInfo final : public TargetInfo {
106100
return 32;
107101
unsigned TargetAS = getTargetAddressSpace(AS);
108102

109-
if (TargetAS == Private || TargetAS == Local)
103+
if (TargetAS == llvm::AMDGPUAS::PRIVATE_ADDRESS ||
104+
TargetAS == llvm::AMDGPUAS::LOCAL_ADDRESS)
110105
return 32;
111106

112107
return 64;
@@ -376,7 +371,7 @@ class LLVM_LIBRARY_VISIBILITY AMDGPUTargetInfo final : public TargetInfo {
376371
}
377372

378373
std::optional<LangAS> getConstantAddressSpace() const override {
379-
return getLangASFromTargetAS(Constant);
374+
return getLangASFromTargetAS(llvm::AMDGPUAS::CONSTANT_ADDRESS);
380375
}
381376

382377
const llvm::omp::GV &getGridValue() const override {
@@ -392,7 +387,7 @@ class LLVM_LIBRARY_VISIBILITY AMDGPUTargetInfo final : public TargetInfo {
392387

393388
/// \returns Target specific vtbl ptr address space.
394389
unsigned getVtblPtrAddressSpace() const override {
395-
return static_cast<unsigned>(Constant);
390+
return static_cast<unsigned>(llvm::AMDGPUAS::CONSTANT_ADDRESS);
396391
}
397392

398393
/// \returns If a target requires an address within a target specific address
@@ -405,9 +400,9 @@ class LLVM_LIBRARY_VISIBILITY AMDGPUTargetInfo final : public TargetInfo {
405400
getDWARFAddressSpace(unsigned AddressSpace) const override {
406401
const unsigned DWARF_Private = 1;
407402
const unsigned DWARF_Local = 2;
408-
if (AddressSpace == Private) {
403+
if (AddressSpace == llvm::AMDGPUAS::PRIVATE_ADDRESS) {
409404
return DWARF_Private;
410-
} else if (AddressSpace == Local) {
405+
} else if (AddressSpace == llvm::AMDGPUAS::LOCAL_ADDRESS) {
411406
return DWARF_Local;
412407
} else {
413408
return std::nullopt;

flang/lib/Frontend/FrontendActions.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
#include "llvm/Passes/PassBuilder.h"
5959
#include "llvm/Passes/PassPlugin.h"
6060
#include "llvm/Passes/StandardInstrumentations.h"
61+
#include "llvm/Support/AMDGPUAddrSpace.h"
6162
#include "llvm/Support/Error.h"
6263
#include "llvm/Support/ErrorHandling.h"
6364
#include "llvm/Support/FileSystem.h"
@@ -166,12 +167,6 @@ static void addAMDGPUSpecificMLIRItems(mlir::ModuleOp &mlirModule,
166167
const llvm::Triple triple(targetOpts.triple);
167168
const llvm::StringRef codeObjectVersionGlobalOpName = "__oclc_ABI_version";
168169

169-
// TODO: Share address spaces enumeration between Clang and Flang.
170-
// Currently this enumeration is defined in Clang specific class
171-
// defined in file: clang/lib/Basic/Targets/AMDGPU.h .
172-
// and we need to move it to LLVM directory.
173-
const int constantAddressSpace = 4;
174-
175170
if (!triple.isAMDGPU()) {
176171
return;
177172
}
@@ -202,7 +197,7 @@ static void addAMDGPUSpecificMLIRItems(mlir::ModuleOp &mlirModule,
202197
originalGVOp.setValueAttr(
203198
builder.getIntegerAttr(int32Type, oclcABIVERsion));
204199
originalGVOp.setUnnamedAddr(mlir::LLVM::UnnamedAddr::Local);
205-
originalGVOp.setAddrSpace(constantAddressSpace);
200+
originalGVOp.setAddrSpace(llvm::AMDGPUAS::CONSTANT_ADDRESS);
206201
originalGVOp.setVisibility_(mlir::LLVM::Visibility::Hidden);
207202
return;
208203
}
@@ -213,7 +208,7 @@ static void addAMDGPUSpecificMLIRItems(mlir::ModuleOp &mlirModule,
213208
/* Name */ codeObjectVersionGlobalOpName,
214209
/* Value */ builder.getIntegerAttr(int32Type, oclcABIVERsion));
215210
covInfo.setUnnamedAddr(mlir::LLVM::UnnamedAddr::Local);
216-
covInfo.setAddrSpace(constantAddressSpace);
211+
covInfo.setAddrSpace(llvm::AMDGPUAS::CONSTANT_ADDRESS);
217212
covInfo.setVisibility_(mlir::LLVM::Visibility::Hidden);
218213
builder.setInsertionPointToStart(mlirModule.getBody());
219214
builder.insert(covInfo);
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
//===---------------- AMDGPUAddrSpace.h -------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
/// \file
10+
/// AMDGPU address space definition
11+
///
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
#ifndef LLVM_SUPPORT_AMDGPUADDRSPACE_H
16+
#define LLVM_SUPPORT_AMDGPUADDRSPACE_H
17+
18+
namespace llvm {
19+
/// OpenCL uses address spaces to differentiate between
20+
/// various memory regions on the hardware. On the CPU
21+
/// all of the address spaces point to the same memory,
22+
/// however on the GPU, each address space points to
23+
/// a separate piece of memory that is unique from other
24+
/// memory locations.
25+
namespace AMDGPUAS {
26+
enum : unsigned {
27+
// The maximum value for flat, generic, local, private, constant and region.
28+
MAX_AMDGPU_ADDRESS = 8,
29+
30+
FLAT_ADDRESS = 0, ///< Address space for flat memory.
31+
GLOBAL_ADDRESS = 1, ///< Address space for global memory (RAT0, VTX0).
32+
REGION_ADDRESS = 2, ///< Address space for region memory. (GDS)
33+
34+
CONSTANT_ADDRESS = 4, ///< Address space for constant memory (VTX2).
35+
LOCAL_ADDRESS = 3, ///< Address space for local memory.
36+
PRIVATE_ADDRESS = 5, ///< Address space for private memory.
37+
38+
CONSTANT_ADDRESS_32BIT = 6, ///< Address space for 32-bit constant memory.
39+
40+
BUFFER_FAT_POINTER = 7, ///< Address space for 160-bit buffer fat pointers.
41+
///< Not used in backend.
42+
43+
BUFFER_RESOURCE = 8, ///< Address space for 128-bit buffer resources.
44+
45+
/// Internal address spaces. Can be freely renumbered.
46+
STREAMOUT_REGISTER = 128, ///< Address space for GS NGG Streamout registers.
47+
/// end Internal address spaces.
48+
49+
/// Address space for direct addressable parameter memory (CONST0).
50+
PARAM_D_ADDRESS = 6,
51+
/// Address space for indirect addressable parameter memory (VTX1).
52+
PARAM_I_ADDRESS = 7,
53+
54+
// Do not re-order the CONSTANT_BUFFER_* enums. Several places depend on
55+
// this order to be able to dynamically index a constant buffer, for
56+
// example:
57+
//
58+
// ConstantBufferAS = CONSTANT_BUFFER_0 + CBIdx
59+
60+
CONSTANT_BUFFER_0 = 8,
61+
CONSTANT_BUFFER_1 = 9,
62+
CONSTANT_BUFFER_2 = 10,
63+
CONSTANT_BUFFER_3 = 11,
64+
CONSTANT_BUFFER_4 = 12,
65+
CONSTANT_BUFFER_5 = 13,
66+
CONSTANT_BUFFER_6 = 14,
67+
CONSTANT_BUFFER_7 = 15,
68+
CONSTANT_BUFFER_8 = 16,
69+
CONSTANT_BUFFER_9 = 17,
70+
CONSTANT_BUFFER_10 = 18,
71+
CONSTANT_BUFFER_11 = 19,
72+
CONSTANT_BUFFER_12 = 20,
73+
CONSTANT_BUFFER_13 = 21,
74+
CONSTANT_BUFFER_14 = 22,
75+
CONSTANT_BUFFER_15 = 23,
76+
77+
// Some places use this if the address space can't be determined.
78+
UNKNOWN_ADDRESS_SPACE = ~0u,
79+
};
80+
} // end namespace AMDGPUAS
81+
} // end namespace llvm
82+
83+
#endif // LLVM_SUPPORT_AMDGPUADDRSPACE_H

llvm/lib/Target/AMDGPU/AMDGPU.h

Lines changed: 1 addition & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include "llvm/IR/PassManager.h"
1414
#include "llvm/Pass.h"
15+
#include "llvm/Support/AMDGPUAddrSpace.h"
1516
#include "llvm/Support/CodeGen.h"
1617

1718
namespace llvm {
@@ -389,72 +390,6 @@ enum TargetIndex {
389390
TI_SCRATCH_RSRC_DWORD2,
390391
TI_SCRATCH_RSRC_DWORD3
391392
};
392-
}
393-
394-
/// OpenCL uses address spaces to differentiate between
395-
/// various memory regions on the hardware. On the CPU
396-
/// all of the address spaces point to the same memory,
397-
/// however on the GPU, each address space points to
398-
/// a separate piece of memory that is unique from other
399-
/// memory locations.
400-
namespace AMDGPUAS {
401-
enum : unsigned {
402-
// The maximum value for flat, generic, local, private, constant and region.
403-
MAX_AMDGPU_ADDRESS = 8,
404-
405-
FLAT_ADDRESS = 0, ///< Address space for flat memory.
406-
GLOBAL_ADDRESS = 1, ///< Address space for global memory (RAT0, VTX0).
407-
REGION_ADDRESS = 2, ///< Address space for region memory. (GDS)
408-
409-
CONSTANT_ADDRESS = 4, ///< Address space for constant memory (VTX2).
410-
LOCAL_ADDRESS = 3, ///< Address space for local memory.
411-
PRIVATE_ADDRESS = 5, ///< Address space for private memory.
412-
413-
CONSTANT_ADDRESS_32BIT = 6, ///< Address space for 32-bit constant memory.
414-
415-
BUFFER_FAT_POINTER = 7, ///< Address space for 160-bit buffer fat pointers.
416-
///< Not used in backend.
417-
418-
BUFFER_RESOURCE = 8, ///< Address space for 128-bit buffer resources.
419-
420-
/// Internal address spaces. Can be freely renumbered.
421-
STREAMOUT_REGISTER = 128, ///< Address space for GS NGG Streamout registers.
422-
/// end Internal address spaces.
423-
424-
/// Address space for direct addressable parameter memory (CONST0).
425-
PARAM_D_ADDRESS = 6,
426-
/// Address space for indirect addressable parameter memory (VTX1).
427-
PARAM_I_ADDRESS = 7,
428-
429-
// Do not re-order the CONSTANT_BUFFER_* enums. Several places depend on
430-
// this order to be able to dynamically index a constant buffer, for
431-
// example:
432-
//
433-
// ConstantBufferAS = CONSTANT_BUFFER_0 + CBIdx
434-
435-
CONSTANT_BUFFER_0 = 8,
436-
CONSTANT_BUFFER_1 = 9,
437-
CONSTANT_BUFFER_2 = 10,
438-
CONSTANT_BUFFER_3 = 11,
439-
CONSTANT_BUFFER_4 = 12,
440-
CONSTANT_BUFFER_5 = 13,
441-
CONSTANT_BUFFER_6 = 14,
442-
CONSTANT_BUFFER_7 = 15,
443-
CONSTANT_BUFFER_8 = 16,
444-
CONSTANT_BUFFER_9 = 17,
445-
CONSTANT_BUFFER_10 = 18,
446-
CONSTANT_BUFFER_11 = 19,
447-
CONSTANT_BUFFER_12 = 20,
448-
CONSTANT_BUFFER_13 = 21,
449-
CONSTANT_BUFFER_14 = 22,
450-
CONSTANT_BUFFER_15 = 23,
451-
452-
// Some places use this if the address space can't be determined.
453-
UNKNOWN_ADDRESS_SPACE = ~0u,
454-
};
455-
}
456-
457-
namespace AMDGPU {
458393

459394
// FIXME: Missing constant_32bit
460395
inline bool isFlatGlobalAddrSpace(unsigned AS) {

0 commit comments

Comments
 (0)