Skip to content

Commit 623bf14

Browse files
authored
[sycl-post-link] Fix spec constant pattern match for DeviceSanitizer (#14740)
Adjust spec constant pattern match for base alloca + offset case in device sanitizer. Address sanitizer merges static allocas into a large layout base alloca and original alloca is replaced with base + offset.
1 parent 914561a commit 623bf14

File tree

5 files changed

+48
-8
lines changed

5 files changed

+48
-8
lines changed

llvm/include/llvm/SYCLLowerIR/SpecConstants.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class SpecConstantsPass : public PassInfoMixin<SpecConstantsPass> {
5959
enum class HandlingMode { default_values, emulation, native };
6060

6161
public:
62-
SpecConstantsPass(HandlingMode Mode) : Mode(Mode) {}
62+
SpecConstantsPass(HandlingMode Mode = HandlingMode::emulation) : Mode(Mode) {}
6363
PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM);
6464

6565
// Searches given module for occurrences of specialization constant-specific
@@ -73,7 +73,7 @@ class SpecConstantsPass : public PassInfoMixin<SpecConstantsPass> {
7373
std::vector<char> &DefaultValues);
7474

7575
private:
76-
HandlingMode Mode = HandlingMode::emulation;
76+
HandlingMode Mode;
7777
};
7878

7979
bool checkModuleContainsSpecConsts(const Module &M);

llvm/lib/Passes/PassBuilder.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@
131131
#include "llvm/SYCLLowerIR/SYCLPropagateAspectsUsage.h"
132132
#include "llvm/SYCLLowerIR/SYCLPropagateJointMatrixUsage.h"
133133
#include "llvm/SYCLLowerIR/SYCLVirtualFunctionsAnalysis.h"
134+
#include "llvm/SYCLLowerIR/SpecConstants.h"
134135
#include "llvm/Support/CommandLine.h"
135136
#include "llvm/Support/Debug.h"
136137
#include "llvm/Support/ErrorHandling.h"

llvm/lib/Passes/PassRegistry.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ MODULE_PASS("lower-slm-reservation-calls", ESIMDLowerSLMReservationCalls())
164164
MODULE_PASS("record-sycl-aspect-names", RecordSYCLAspectNamesPass())
165165
MODULE_PASS("sycl-virtual-functions-analysis",
166166
SYCLVirtualFunctionsAnalysisPass())
167+
MODULE_PASS("spec-constants", SpecConstantsPass())
167168
#undef MODULE_PASS
168169

169170
#ifndef MODULE_PASS_WITH_PARAMS

llvm/lib/SYCLLowerIR/SpecConstants.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "llvm/IR/Instructions.h"
2121
#include "llvm/IR/IntrinsicInst.h"
2222
#include "llvm/IR/Operator.h"
23+
#include "llvm/IR/PatternMatch.h"
2324
#include "llvm/TargetParser/Triple.h"
2425

2526
#include <vector>
@@ -101,12 +102,16 @@ StringRef getStringLiteralArg(const CallInst *CI, unsigned ArgNo,
101102
// so that %1 is trivially known to be the address of the @.str literal.
102103

103104
Value *TmpPtr = L->getPointerOperand();
104-
AssertRelease((isa<AddrSpaceCastInst>(TmpPtr) &&
105-
isa<AllocaInst>(cast<AddrSpaceCastInst>(TmpPtr)
106-
->getPointerOperand()
107-
->stripPointerCasts())) ||
108-
isa<AllocaInst>(TmpPtr),
109-
"unexpected instruction type");
105+
auto ValueIsAlloca = [](Value *V) {
106+
if (auto *ASC = dyn_cast<AddrSpaceCastInst>(V))
107+
V = ASC->getPointerOperand()->stripPointerCasts();
108+
using namespace PatternMatch;
109+
Value *X;
110+
if (match(V, m_IntToPtr(m_Add(m_PtrToInt(m_Value(X)), m_ConstantInt()))))
111+
V = X;
112+
return isa<AllocaInst>(V);
113+
};
114+
AssertRelease(ValueIsAlloca(TmpPtr), "unexpected instruction type");
110115

111116
// find the store of the literal address into TmpPtr
112117
StoreInst *Store = nullptr;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
; RUN: opt -passes=spec-constants %s -S -o - | FileCheck %s
2+
3+
; Check there is no assert error when literal address is loaded from an alloca
4+
; with offset.
5+
6+
target datalayout = "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024-n8:16:32:64-G1"
7+
target triple = "spir64-unknown-unknown"
8+
9+
%"class.sycl::_V1::specialization_id" = type { i32 }
10+
11+
@_ZL9test_id_1 = addrspace(1) constant %"class.sycl::_V1::specialization_id" { i32 42 }
12+
@__usid_str = constant [36 x i8] c"uide7faddc6b4d2fe92____ZL9test_id_1\00"
13+
14+
define spir_func void @_ZZZ4mainENKUlRN4sycl3_V17handlerEE_clES2_ENKUlNS0_14kernel_handlerEE_clES4_(ptr addrspace(4) %this1.i7) {
15+
entry:
16+
%MyAlloca = alloca i8, i64 224, align 32
17+
%0 = ptrtoint ptr %MyAlloca to i64
18+
%1 = add i64 %0, 96
19+
%2 = inttoptr i64 %1 to ptr
20+
%SymbolicID.ascast.i = addrspacecast ptr %2 to ptr addrspace(4)
21+
store ptr addrspace(4) addrspacecast (ptr @__usid_str to ptr addrspace(4)), ptr addrspace(4) %SymbolicID.ascast.i, align 8
22+
%3 = load ptr addrspace(4), ptr addrspace(4) %SymbolicID.ascast.i, align 8
23+
%4 = load ptr addrspace(4), ptr addrspace(4) %this1.i7, align 8
24+
25+
; CHECK-NOT: call spir_func noundef i32 @_Z37__sycl_getScalar2020SpecConstantValueIiET_PKcPKvS4_(
26+
; CHECK: %conv = sitofp i32 %load to double
27+
28+
%call.i8 = call spir_func i32 @_Z37__sycl_getScalar2020SpecConstantValueIiET_PKcPKvS4_(ptr addrspace(4) %3, ptr addrspace(4) addrspacecast (ptr addrspace(1) @_ZL9test_id_1 to ptr addrspace(4)), ptr addrspace(4) %4)
29+
%conv = sitofp i32 %call.i8 to double
30+
ret void
31+
}
32+
33+
declare spir_func i32 @_Z37__sycl_getScalar2020SpecConstantValueIiET_PKcPKvS4_(ptr addrspace(4), ptr addrspace(4), ptr addrspace(4))

0 commit comments

Comments
 (0)