Skip to content

Commit

Permalink
Add support for multiple Intel cache controls on a single argument (#…
Browse files Browse the repository at this point in the history
…2599)

In this case zero GEP will be created a single time, all the decorations will be attached to it.

Signed-off-by: Sidorov, Dmitry <dmitry.sidorov@intel.com>
  • Loading branch information
MrSidims authored Jun 17, 2024
1 parent bd0fffe commit 9aa3c9c
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 7 deletions.
28 changes: 23 additions & 5 deletions lib/SPIRV/SPIRVRegularizeLLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,7 @@ void prepareCacheControlsTranslation(Metadata *MD, Instruction *Inst) {
return;
auto *ArgDecoMD = dyn_cast<MDNode>(MD);
assert(ArgDecoMD && "Decoration list must be a metadata node");
std::vector<Instruction *> CreatedGeps;
for (unsigned I = 0, E = ArgDecoMD->getNumOperands(); I != E; ++I) {
auto *DecoMD = dyn_cast<MDNode>(ArgDecoMD->getOperand(I));
if (!DecoMD) {
Expand Down Expand Up @@ -536,19 +537,36 @@ void prepareCacheControlsTranslation(Metadata *MD, Instruction *Inst) {

// Create dummy GEP for SSA copy of the pointer operand. Lets do our best
// to guess pointee type here, but if we won't - just pointer is also fine,
// if necessary TypeScavenger will adjust types and create bitcasts.
// if necessary TypeScavenger will adjust types and create bitcasts. If
// memory instruction operand is already created zero GEP - create nothing
// and use the old GEP.
SmallVector<Metadata *, 4> MDs;
std::vector<Metadata *> OPs = {KindMD, LevelMD, ControlMD};
if (auto *const GEP = dyn_cast<GetElementPtrInst>(PtrInstOp)) {
if (GEP->hasAllZeroIndices() &&
(std::find(CreatedGeps.begin(), CreatedGeps.end(), GEP) !=
std::end(CreatedGeps))) {
MDs.push_back(MDNode::get(Inst->getContext(), OPs));
// If the existing GEP has SPIRV_MD_DECORATIONS metadata - copy it
if (auto *OldMD = GEP->getMetadata(SPIRV_MD_DECORATIONS))
for (unsigned I = 0, E = OldMD->getNumOperands(); I != E; ++I)
if (auto *DecoMD = dyn_cast<MDNode>(OldMD->getOperand(I)))
MDs.push_back(DecoMD);
MDNode *MDList = MDNode::get(Inst->getContext(), MDs);
GEP->setMetadata(SPIRV_MD_DECORATIONS, MDList);
return;
}
}
IRBuilder Builder(Inst);
Type *GEPTy = PtrInstOp->getType();
Type *GEPTy = Builder.getInt8Ty();
if (auto *LI = dyn_cast<LoadInst>(Inst))
GEPTy = LI->getType();
else if (auto *SI = dyn_cast<StoreInst>(Inst))
GEPTy = SI->getValueOperand()->getType();
auto *GEP =
cast<Instruction>(Builder.CreateConstGEP1_32(GEPTy, PtrInstOp, 0));
CreatedGeps.push_back(GEP);
Inst->setOperand(TargetArgNo, GEP);

SmallVector<Metadata *, 4> MDs;
std::vector<Metadata *> OPs = {KindMD, LevelMD, ControlMD};
MDs.push_back(MDNode::get(Inst->getContext(), OPs));
MDNode *MDList = MDNode::get(Inst->getContext(), MDs);
GEP->setMetadata(SPIRV_MD_DECORATIONS, MDList);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
; CHECK-SPIRV: PtrAccessChain [[#]] [[#GEP2]] [[#]] [[#Zero]]
; CHECK-SPIRV: FunctionCall [[#]] [[#]] [[#]] [[#GEP1]] [[#GEP2]]

; CHECK-LLVM: %[[#GEP1:]] = getelementptr ptr addrspace(1), ptr addrspace(1) %{{.*}}, i32 0, !spirv.Decorations ![[#Cache1:]]
; CHECK-LLVM: %[[#GEP2:]] = getelementptr ptr addrspace(1), ptr addrspace(1) %{{.*}}, i32 0, !spirv.Decorations ![[#Cache2:]]
; CHECK-LLVM: %[[#GEP1:]] = getelementptr i8, ptr addrspace(1) %{{.*}}, i32 0, !spirv.Decorations ![[#Cache1:]]
; CHECK-LLVM: %[[#GEP2:]] = getelementptr i8, ptr addrspace(1) %{{.*}}, i32 0, !spirv.Decorations ![[#Cache2:]]
; CHECK-LLVM: call spir_func void @foo(ptr addrspace(1) %[[#GEP1]], ptr addrspace(1) %[[#GEP2]])
; CHECK-LLVM: ![[#Cache1]] = !{![[#LoadCache:]]}
; CHECK-LLVM: ![[#LoadCache]] = !{i32 6442, i32 0, i32 1}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
; RUN: llvm-as %s -o %t.bc
; RUN: llvm-spirv --spirv-ext=+SPV_INTEL_cache_controls -spirv-text %t.bc -o - | FileCheck %s --check-prefix=CHECK-SPIRV
; RUN: llvm-spirv --spirv-ext=+SPV_INTEL_cache_controls %t.bc -o %t.spv
; RUN: llvm-spirv -r %t.spv --spirv-target-env=SPV-IR -o - | llvm-dis -o - | FileCheck %s --check-prefix=CHECK-LLVM

; CHECK-SPIRV-DAG: Name [[#Func:]] "test"
; CHECK-SPIRV-DAG: Name [[#FuncGEP:]] "test_gep"
; CHECK-SPIRV-DAG: TypeInt [[#Int32:]] 32 0
; CHECK-SPIRV-DAG: Constant [[#Int32]] [[#Zero:]] 0
; CHECK-SPIRV-DAG: Decorate [[#GEP1:]] CacheControlLoadINTEL 1 1
; CHECK-SPIRV-DAG: Decorate [[#GEP1]] CacheControlLoadINTEL 0 3
; CHECK-SPIRV-DAG: Decorate [[#GEP2:]] CacheControlLoadINTEL 1 1
; CHECK-SPIRV-DAG: Decorate [[#GEP2]] CacheControlLoadINTEL 0 3

; CHECK-SPIRV: Function [[#]] [[#Func]]
; CHECK-SPIRV: FunctionParameter [[#]] [[#Buffer:]]
; CHECK-SPIRV: PtrAccessChain [[#]] [[#GEP1]] [[#Buffer]] [[#Zero]]
; CHECK-SPIRV: FunctionCall [[#]] [[#]] [[#]] [[#GEP1]]

; CHECK-SPIRV: Function [[#]] [[#FuncGEP]]
; CHECK-SPIRV: FunctionParameter [[#]] [[#Buffer:]]
; CHECK-SPIRV: PtrAccessChain [[#]] [[#GEP3:]] [[#Buffer]] [[#Zero]]
; CHECK-SPIRV: Bitcast [[#]] [[#BitCast:]] [[#GEP3]]
; CHECK-SPIRV: PtrAccessChain [[#]] [[#GEP2]] [[#BitCast:]] [[#Zero]]
; CHECK-SPIRV: FunctionCall [[#]] [[#]] [[#]] [[#GEP2]]

; CHECK-LLVM: define spir_kernel void @test(ptr addrspace(1) %[[Param1:[a-z0-9_.]+]])
; CHECK-LLVM: %[[#GEP:]] = getelementptr i8, ptr addrspace(1) %[[Param1]], i32 0, !spirv.Decorations ![[#MD:]]
; CHECK-LLVM: call spir_func void @foo(ptr addrspace(1) %[[#GEP:]])

; CHECK-LLVM: define spir_kernel void @test_gep(ptr addrspace(1) %[[Param2:[a-z0-9_.]+]])
; CHECK-LLVM: %[[#GEP1:]] = getelementptr ptr addrspace(1), ptr addrspace(1) %[[Param2]], i32 0
; CHECK-LLVM: %[[#BitCast:]] = bitcast ptr addrspace(1) %[[#GEP1]] to ptr addrspace(1)
; CHECK-LLVM: %[[#GEP2:]] = getelementptr i8, ptr addrspace(1) %[[#BitCast]], i32 0, !spirv.Decorations ![[#MD]]
; CHECK-LLVM: call spir_func void @foo(ptr addrspace(1) %[[#GEP2:]])

; CHECK-LLVM: ![[#MD]] = !{![[#Dec1:]], ![[#Dec2:]]}
; CHECK-LLVM: ![[#Dec1]] = !{i32 6442, i32 1, i32 1}
; CHECK-LLVM: ![[#Dec2]] = !{i32 6442, i32 0, i32 3}

target triple = "spir64-unknown-unknown"

define spir_kernel void @test(ptr addrspace(1) %buffer1) {
entry:
call void @foo(ptr addrspace(1) %buffer1), !spirv.DecorationCacheControlINTEL !3
ret void
}

define spir_kernel void @test_gep(ptr addrspace(1) %buffer1) {
entry:
%0 = getelementptr ptr addrspace(1), ptr addrspace(1) %buffer1, i32 0
call void @foo(ptr addrspace(1) %0), !spirv.DecorationCacheControlINTEL !3
ret void
}

declare void @foo(ptr addrspace(1))

!spirv.MemoryModel = !{!0}
!spirv.Source = !{!1}
!opencl.spir.version = !{!2}
!opencl.ocl.version = !{!2}

!0 = !{i32 2, i32 2}
!1 = !{i32 3, i32 102000}
!2 = !{i32 1, i32 2}
!3 = !{!4, !5}
!4 = !{i32 6442, i32 0, i32 3, i32 0}
!5 = !{i32 6442, i32 1, i32 1, i32 0}

0 comments on commit 9aa3c9c

Please sign in to comment.