Skip to content

Commit 67dea8f

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

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

llvm/include/llvm/IR/DataLayout.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "llvm/ADT/STLExtras.h"
2525
#include "llvm/ADT/SmallVector.h"
2626
#include "llvm/ADT/StringRef.h"
27+
#include "llvm/ADT/DenseMap.h"
2728
#include "llvm/IR/DerivedTypes.h"
2829
#include "llvm/IR/Type.h"
2930
#include "llvm/Support/Alignment.h"
@@ -164,6 +165,8 @@ class DataLayout {
164165
/// well-defined bitwise representation.
165166
SmallVector<unsigned, 8> NonIntegralAddressSpaces;
166167

168+
DenseMap<uint64_t, uint64_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+
uint64_t getNonZeroValueForAddrSpace(uint64_t AddrSpace) {
306+
auto It = AddrSpaceToNonZeroValueMap.find(AddrSpace);
307+
if (It == AddrSpaceToNonZeroValueMap.end())
308+
return 0x000000000;
309+
return AddrSpaceToNonZeroValueMap[AddrSpace];
310+
}
311+
312+
void setNonZeroValueForAddrSpace(uint64_t AddrSpace, uint64_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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,27 @@ Error DataLayout::parseSpecifier(StringRef Desc) {
502502
return Err;
503503
break;
504504
}
505+
case 'z': {
506+
uint64_t AddrSpace = 0;
507+
if (!Tok.empty())
508+
if (Error Err = getInt(Tok, AddrSpace))
509+
return Err;
510+
if (!isUInt<24>(AddrSpace))
511+
return reportError("Invalid address space, must be a 24-bit integer");
512+
513+
if (Rest.empty())
514+
return reportError(
515+
"Missing address space specification for pointer in datalayout string");
516+
if (Error Err = ::split(Rest, ':', Split))
517+
return Err;
518+
uint64_t Value;
519+
if (Error Err = getInt(Tok, Value))
520+
return Err;
521+
522+
setNonZeroValueForAddrSpace(AddrSpace, Value);
523+
524+
break;
525+
}
505526
case 'G': { // Default address space for global variables.
506527
if (Error Err = getAddrSpace(Tok, DefaultGlobalsAddrSpace))
507528
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-z3:1-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-z3:1-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)