Skip to content

Commit 33150bf

Browse files
authored
Fix OCL builtins translation in case SPV_KHR_untyped_pointers is enabled (#2723)
This change allows to preserve the correct builtin mangling in reverse translation.
1 parent e16f698 commit 33150bf

File tree

4 files changed

+42
-16
lines changed

4 files changed

+42
-16
lines changed

lib/SPIRV/SPIRVReader.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4833,6 +4833,18 @@ Instruction *SPIRVToLLVM::transOCLBuiltinFromExtInst(SPIRVExtInst *BC,
48334833
"Not OpenCL extended instruction");
48344834

48354835
std::vector<Type *> ArgTypes = transTypeVector(BC->getArgTypes(), true);
4836+
for (unsigned I = 0; I < ArgTypes.size(); I++) {
4837+
// Special handling for "truly" untyped pointers to preserve correct OCL
4838+
// bultin mangling.
4839+
if (isa<PointerType>(ArgTypes[I]) &&
4840+
BC->getArgValue(I)->isUntypedVariable()) {
4841+
auto *BVar = static_cast<SPIRVUntypedVariableKHR *>(BC->getArgValue(I));
4842+
ArgTypes[I] = TypedPointerType::get(
4843+
transType(BVar->getDataType()),
4844+
SPIRSPIRVAddrSpaceMap::rmap(BVar->getStorageClass()));
4845+
}
4846+
}
4847+
48364848
Type *RetTy = transType(BC->getType());
48374849
std::string MangledName =
48384850
getSPIRVFriendlyIRFunctionName(ExtOp, ArgTypes, RetTy);

lib/SPIRV/libSPIRV/SPIRVEntry.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ class SPIRVEntry {
342342
bool isVariable() const {
343343
return OpCode == OpVariable || OpCode == OpUntypedVariableKHR;
344344
}
345+
bool isUntypedVariable() const { return OpCode == OpUntypedVariableKHR; }
345346
bool isEndOfBlock() const;
346347
virtual bool isInst() const { return false; }
347348
virtual bool isOperandLiteral(unsigned Index) const {

lib/SPIRV/libSPIRV/SPIRVInstruction.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1995,14 +1995,16 @@ class SPIRVExtInst : public SPIRVFunctionCallGeneric<OpExtInst, 5> {
19951995
}
19961996
std::vector<SPIRVValue *> getArgValues() {
19971997
std::vector<SPIRVValue *> VArgs;
1998-
for (size_t I = 0; I < Args.size(); ++I) {
1999-
if (isOperandLiteral(I))
2000-
VArgs.push_back(Module->getLiteralAsConstant(Args[I]));
2001-
else
2002-
VArgs.push_back(getValue(Args[I]));
2003-
}
1998+
for (size_t I = 0; I < Args.size(); ++I)
1999+
VArgs.push_back(getArgValue(I));
20042000
return VArgs;
20052001
}
2002+
SPIRVValue *getArgValue(SPIRVWord I) {
2003+
if (isOperandLiteral(I))
2004+
return Module->getLiteralAsConstant(Args[I]);
2005+
return getValue(Args[I]);
2006+
}
2007+
20062008
std::vector<SPIRVType *> getArgTypes() {
20072009
std::vector<SPIRVType *> ArgTypes;
20082010
auto VArgs = getArgValues();

test/transcoding/float16.ll

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,34 @@
11
; RUN: llvm-as %s -o %t.bc
22
; RUN: llvm-spirv %t.bc -o %t.spv
33
; RUN: spirv-val %t.spv
4-
; RUN: llvm-spirv %t.spv -to-text -o - | FileCheck %s --check-prefix=CHECK-SPIRV
4+
; RUN: llvm-spirv %t.spv -to-text -o - | FileCheck %s --check-prefixes=CHECK-SPIRV,CHECK-SPIRV-NOEXT
5+
; RUN: llvm-spirv -r %t.spv -o %t.rev.bc
6+
; RUN: llvm-dis %t.rev.bc -o - | FileCheck %s --check-prefix=CHECK-LLVM
7+
8+
; Verify that even though we use the fract instruction with untyped pointers enabled,
9+
; the SPV binary is valid and we get exactly the same output IR after the reverse translation.
10+
; RUN: llvm-spirv %t.bc -o %t.spv --spirv-ext=+SPV_KHR_untyped_pointers
11+
; TODO: enable back once spirv-tools are updated
12+
; R/UN: spirv-val %t.spv
13+
; RUN: llvm-spirv %t.spv -to-text -o - | FileCheck %s --check-prefixes=CHECK-SPIRV,CHECK-SPIRV-EXT
514
; RUN: llvm-spirv -r %t.spv -o %t.rev.bc
615
; RUN: llvm-dis %t.rev.bc -o - | FileCheck %s --check-prefix=CHECK-LLVM
716

817
source_filename = "math_builtin_float_half.cpp"
918
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"
1019
target triple = "spirv64-unknown-unknown"
1120

12-
; CHECK-SPIRV: 3 TypeFloat [[HALF:[0-9]+]] 16
13-
; CHECK-SPIRV: 4 TypePointer [[HALFPTR:[0-9]+]] 7 [[HALF]]
14-
; CHECK-SPIRV: 4 TypeVector [[HALFV2:[0-9]+]] [[HALF]] 2
15-
; CHECK-SPIRV: 4 TypePointer [[HALFV2PTR:[0-9]+]] 7 [[HALFV2]]
16-
; CHECK-SPIRV: 4 Constant [[HALF]] [[CONST:[0-9]+]] 14788
17-
; CHECK-SPIRV: 4 Variable [[HALFPTR]] [[ADDR:[0-9]+]] 7
18-
; CHECK-SPIRV: 4 Variable [[HALFV2PTR]] [[ADDR2:[0-9]+]] 7
19-
; CHECK-SPIRV: 7 ExtInst [[HALF]] [[#]] 1 fract [[CONST]] [[ADDR]]
20-
; CHECK-SPIRV: 7 ExtInst [[HALFV2]] [[#]] 1 fract [[#]] [[ADDR2]]
21+
; CHECK-SPIRV: TypeFloat [[HALF:[0-9]+]] 16
22+
; CHECK-SPIRV-NOEXT: TypePointer [[HALFPTR:[0-9]+]] 7 [[HALF]]
23+
; CHECK-SPIRV-EXT: TypeUntypedPointerKHR [[HALFPTR:[0-9]+]] 7
24+
; CHECK-SPIRV: TypeVector [[HALFV2:[0-9]+]] [[HALF]] 2
25+
; CHECK-SPIRV: TypePointer [[HALFV2PTR:[0-9]+]] 7 [[HALFV2]]
26+
; CHECK-SPIRV: Constant [[HALF]] [[CONST:[0-9]+]] 14788
27+
; CHECK-SPIRV-NOEXT: Variable [[HALFPTR]] [[ADDR:[0-9]+]] 7
28+
; CHECK-SPIRV-EXT: UntypedVariableKHR [[HALFPTR]] [[ADDR:[0-9]+]] 7 [[HALF]]
29+
; CHECK-SPIRV: Variable [[HALFV2PTR]] [[ADDR2:[0-9]+]] 7
30+
; CHECK-SPIRV: ExtInst [[HALF]] [[#]] 1 fract [[CONST]] [[ADDR]]
31+
; CHECK-SPIRV: ExtInst [[HALFV2]] [[#]] 1 fract [[#]] [[ADDR2]]
2132

2233
; CHECK-LLVM: %addr = alloca half
2334
; CHECK-LLVM: %addr2 = alloca <2 x half>

0 commit comments

Comments
 (0)