Skip to content

Commit a1d512c

Browse files
committed
[WIP] Extend data layout to add non zero null value for address space
1 parent 3356818 commit a1d512c

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

llvm/include/llvm/IR/DataLayout.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include "llvm/ADT/APInt.h"
2323
#include "llvm/ADT/ArrayRef.h"
24+
#include "llvm/ADT/DenseMap.h"
2425
#include "llvm/ADT/STLExtras.h"
2526
#include "llvm/ADT/SmallVector.h"
2627
#include "llvm/ADT/StringRef.h"
@@ -164,6 +165,8 @@ class DataLayout {
164165
/// well-defined bitwise representation.
165166
SmallVector<unsigned, 8> NonIntegralAddressSpaces;
166167

168+
DenseMap<unsigned, int64_t> AddrSpaceToNonZeroValueMap;
169+
167170
/// Attempts to set the alignment of the given type. Returns an error
168171
/// description on failure.
169172
Error setAlignment(AlignTypeEnum AlignType, Align ABIAlign, Align PrefAlign,
@@ -299,6 +302,17 @@ class DataLayout {
299302
return ManglingMode == MM_WinCOFFX86;
300303
}
301304

305+
int64_t getNullPointerValue(unsigned AddrSpace) {
306+
auto It = AddrSpaceToNonZeroValueMap.find(AddrSpace);
307+
if (It == AddrSpaceToNonZeroValueMap.end())
308+
return 0;
309+
return It->second;
310+
}
311+
312+
void setNullPointerValue(unsigned AddrSpace, int64_t Value) {
313+
AddrSpaceToNonZeroValueMap[AddrSpace] = Value;
314+
}
315+
302316
/// Returns true if symbols with leading question marks should not receive IR
303317
/// mangling. True for Windows mangling modes.
304318
bool doNotMangleLeadingQuestionMark() const {

llvm/lib/IR/DataLayout.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,8 @@ void DataLayout::reset(StringRef Desc) {
216216
if (Error Err = setPointerAlignmentInBits(0, Align(8), Align(8), 64, 64))
217217
return report_fatal_error(std::move(Err));
218218

219+
setNullPointerValue(INT_MAX, 0);
220+
219221
if (Error Err = parseSpecifier(Desc))
220222
return report_fatal_error(std::move(Err));
221223
}
@@ -251,6 +253,15 @@ template <typename IntTy> static Error getInt(StringRef R, IntTy &Result) {
251253
return Error::success();
252254
}
253255

256+
template <typename IntTy>
257+
static Error getIntForAddrSpace(StringRef R, IntTy &Result) {
258+
if (R.equals("neg"))
259+
Result = -1;
260+
else
261+
return getInt<IntTy>(R, Result);
262+
return Error::success();
263+
}
264+
254265
/// Get an unsigned integer representing the number of bits and convert it into
255266
/// bytes. Error out of not a byte width multiple.
256267
template <typename IntTy>
@@ -502,6 +513,31 @@ Error DataLayout::parseSpecifier(StringRef Desc) {
502513
return Err;
503514
break;
504515
}
516+
case 'z': {
517+
unsigned AddrSpace = 0;
518+
if (!Tok.empty())
519+
if (Error Err = getInt(Tok, AddrSpace))
520+
return Err;
521+
if (!isUInt<24>(AddrSpace))
522+
return reportError("Invalid address space, must be a 24-bit integer");
523+
524+
if (Rest.empty())
525+
return reportError("Missing address space specification for pointer in "
526+
"datalayout string");
527+
if (Error Err = ::split(Rest, ':', Split))
528+
return Err;
529+
int64_t Value;
530+
if (Error Err = getIntForAddrSpace(Tok, Value))
531+
return Err;
532+
533+
// for default address space e.g., z:0
534+
if (AddrSpace == 0) {
535+
setNullPointerValue(INT_MAX, 0);
536+
break;
537+
}
538+
setNullPointerValue(AddrSpace, Value);
539+
break;
540+
}
505541
case 'G': { // Default address space for global variables.
506542
if (Error Err = getAddrSpace(Tok, DefaultGlobalsAddrSpace))
507543
return Err;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 4
2+
; RUN: opt < %s -S -mtriple=amdgcn-- | FileCheck %s
3+
4+
; CHECK: target datalayout = "e-p:64:64-p1:64:64-p2:32:32-p3:32:32-p4:64:64-p5:32:32-p6:32:32-p7:160:256:256:32-p8:128:128-p9:192:256:256:32-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024-v2048:2048-n32:64-z:0-z2:neg-z3:neg-z5:neg-S32-A5-G1-ni:7:8:9"
5+
target datalayout = "e-p:64:64-p1:64:64-p2:32:32-p3:32:32-p4:64:64-p5:32:32-p6:32:32-p7:160:256:256:32-p8:128:128-p9:192:256:256:32-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024-v2048:2048-n32:64-z:0-z2:neg-z3:neg-z5:neg-S32-A5-G1-ni:7:8:9"
6+
@lds = addrspace(3) global [8 x i32] [i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7, i32 8]
7+
8+
define amdgpu_kernel void @load_init_lds_global(ptr addrspace(1) %out, i1 %p) {
9+
; CHECK-LABEL: define amdgpu_kernel void @load_init_lds_global(
10+
; CHECK-SAME: ptr addrspace(1) [[OUT:%.*]], i1 [[P:%.*]]) {
11+
; CHECK-NEXT: [[GEP:%.*]] = getelementptr [8 x i32], ptr addrspace(3) @lds, i32 0, i32 10
12+
; CHECK-NEXT: [[LD:%.*]] = load i32, ptr addrspace(3) [[GEP]], align 4
13+
; CHECK-NEXT: store i32 [[LD]], ptr addrspace(1) [[OUT]], align 4
14+
; CHECK-NEXT: ret void
15+
;
16+
%gep = getelementptr [8 x i32], ptr addrspace(3) @lds, i32 0, i32 10
17+
%ld = load i32, ptr addrspace(3) %gep
18+
store i32 %ld, ptr addrspace(1) %out
19+
ret void
20+
}

0 commit comments

Comments
 (0)