From 9207ef2aa1507733cb0e3d5f4465c1b977dc2a6e Mon Sep 17 00:00:00 2001 From: Viktoria Maximova Date: Fri, 22 Nov 2024 10:42:09 +0100 Subject: [PATCH] Fix translation of `load` instruction used with `SPV_KHR_untyped_pointers` (#2867) Do not lose variable type in forward translation - take it from the already translated "untyped" variable. --- lib/SPIRV/SPIRVWriter.cpp | 42 ++++++++++++------- ...arbitrary-precision-fixed-point-numbers.ll | 9 +++- ...lity-arbitrary-precision-floating-point.ll | 5 +++ test/group_non_uniform_shuffle_down.ll | 17 ++++++-- 4 files changed, 53 insertions(+), 20 deletions(-) diff --git a/lib/SPIRV/SPIRVWriter.cpp b/lib/SPIRV/SPIRVWriter.cpp index 6f717e2e35..55af2aad64 100644 --- a/lib/SPIRV/SPIRVWriter.cpp +++ b/lib/SPIRV/SPIRVWriter.cpp @@ -2251,11 +2251,10 @@ LLVMToSPIRVBase::transValueWithoutDecoration(Value *V, SPIRVBasicBlock *BB, static_cast(FrexpResult)->getArgValues()[1]; SPIRVType *LoadTy = nullptr; - if (IntFromFrexpResult->isUntypedVariable()) { - auto *BV = - static_cast(IntFromFrexpResult); - LoadTy = BV->getDataType(); - } + if (IntFromFrexpResult->isUntypedVariable()) + LoadTy = static_cast(IntFromFrexpResult) + ->getDataType(); + IntFromFrexpResult = BM->addLoadInst(IntFromFrexpResult, {}, BB, LoadTy); @@ -2483,10 +2482,10 @@ LLVMToSPIRVBase::transValueWithoutDecoration(Value *V, SPIRVBasicBlock *BB, SPIRVValue *IntFromFrexpResult = static_cast(Val)->getArgValues()[1]; SPIRVType *LoadTy = nullptr; - if (IntFromFrexpResult->isUntypedVariable()) { - auto *BV = static_cast(IntFromFrexpResult); - LoadTy = BV->getDataType(); - } + if (IntFromFrexpResult->isUntypedVariable()) + LoadTy = static_cast(IntFromFrexpResult) + ->getDataType(); + IntFromFrexpResult = BM->addLoadInst(IntFromFrexpResult, {}, BB, LoadTy); return mapValue(V, IntFromFrexpResult); @@ -6499,8 +6498,11 @@ LLVMToSPIRVBase::transBuiltinToInstWithoutDecoration(Op OC, CallInst *CI, // Input - integer input of any width or 'byval' pointer to this integer SPIRVValue *Input = transValue(*OpItr, BB); + SPIRVType *LoadTy = nullptr; + if (Input->isUntypedVariable()) + LoadTy = static_cast(Input)->getDataType(); if (OpItr->getType()->isPointerTy()) - Input = BM->addLoadInst(Input, {}, BB); + Input = BM->addLoadInst(Input, {}, BB, LoadTy); OpItr++; std::vector Literals; @@ -6590,8 +6592,11 @@ LLVMToSPIRVBase::transBuiltinToInstWithoutDecoration(Op OC, CallInst *CI, // Input - integer input of any width or 'byval' pointer to this integer SPIRVValue *Input = transValue(*OpItr, BB); + SPIRVType *LoadTy = nullptr; + if (Input->isUntypedVariable()) + LoadTy = static_cast(Input)->getDataType(); if (OpItr->getType()->isPointerTy()) - Input = BM->addLoadInst(Input, {}, BB); + Input = BM->addLoadInst(Input, {}, BB, LoadTy); OpItr++; std::vector Literals; @@ -6667,8 +6672,11 @@ LLVMToSPIRVBase::transBuiltinToInstWithoutDecoration(Op OC, CallInst *CI, // InA - integer input of any width or 'byval' pointer to this integer SPIRVValue *InA = transValue(*OpItr, BB); + SPIRVType *LoadTy = nullptr; + if (InA->isUntypedVariable()) + LoadTy = static_cast(InA)->getDataType(); if (OpItr->getType()->isPointerTy()) - InA = BM->addLoadInst(InA, {}, BB); + InA = BM->addLoadInst(InA, {}, BB, LoadTy); OpItr++; std::vector Literals; @@ -6676,9 +6684,12 @@ LLVMToSPIRVBase::transBuiltinToInstWithoutDecoration(Op OC, CallInst *CI, // InB - integer input of any width or 'byval' pointer to this integer SPIRVValue *InB = transValue(*OpItr, BB); + LoadTy = nullptr; + if (InB->isUntypedVariable()) + LoadTy = static_cast(InB)->getDataType(); if (OpItr->getType()->isPointerTy()) { std::vector Mem; - InB = BM->addLoadInst(InB, Mem, BB); + InB = BM->addLoadInst(InB, Mem, BB, LoadTy); } OpItr++; @@ -6758,7 +6769,10 @@ LLVMToSPIRVBase::transBuiltinToInstWithoutDecoration(Op OC, CallInst *CI, transValue(CI->getArgOperand(0)->stripPointerCasts(), BB); SPIRVId ScopeId = transValue(CI->getArgOperand(1), BB)->getId(); SPIRVValue *Delta = transValue(CI->getArgOperand(3), BB); - SPIRVValue *Composite0 = BM->addLoadInst(InValue, {}, BB); + SPIRVType *LoadTy = nullptr; + if (InValue->isUntypedVariable()) + LoadTy = static_cast(InValue)->getDataType(); + SPIRVValue *Composite0 = BM->addLoadInst(InValue, {}, BB, LoadTy); Type *MemberTy = St->getElementType(0); SPIRVType *ElementTy = transType(MemberTy); SPIRVValue *Element0 = diff --git a/test/extensions/INTEL/SPV_INTEL_arbitrary_precision_fixed_point/capability-arbitrary-precision-fixed-point-numbers.ll b/test/extensions/INTEL/SPV_INTEL_arbitrary_precision_fixed_point/capability-arbitrary-precision-fixed-point-numbers.ll index 8e4c2512a3..041874085b 100644 --- a/test/extensions/INTEL/SPV_INTEL_arbitrary_precision_fixed_point/capability-arbitrary-precision-fixed-point-numbers.ll +++ b/test/extensions/INTEL/SPV_INTEL_arbitrary_precision_fixed_point/capability-arbitrary-precision-fixed-point-numbers.ll @@ -100,8 +100,13 @@ ; CHECK-ERROR: InvalidInstruction: Can't translate llvm instruction: ; CHECK-ERROR: Fixed point instructions can't be translated correctly without enabled SPV_INTEL_arbitrary_precision_fixed_point extension! -; RUN: llvm-spirv -r %t.spv -o %t.bc -; RUN: llvm-dis < %t.bc | FileCheck %s --check-prefix=CHECK-LLVM +; RUN: llvm-spirv -r %t.spv -o %t.rev.bc +; RUN: llvm-dis < %t.rev.bc | FileCheck %s --check-prefix=CHECK-LLVM + +; Test with untyped pointers enabled. +; RUN: llvm-spirv %t.bc --spirv-ext=+SPV_INTEL_arbitrary_precision_integers,+SPV_INTEL_arbitrary_precision_fixed_point,+SPV_KHR_untyped_pointers -o %t.spv +; RUN: llvm-spirv -r %t.spv -o %t.rev.bc +; RUN: llvm-dis < %t.rev.bc | FileCheck %s --check-prefix=CHECK-LLVM ; CHECK-SPIRV: 2 Capability Kernel ; CHECK-SPIRV: 2 Capability ArbitraryPrecisionIntegersINTEL diff --git a/test/extensions/INTEL/SPV_INTEL_arbitrary_precision_floating_point/capability-arbitrary-precision-floating-point.ll b/test/extensions/INTEL/SPV_INTEL_arbitrary_precision_floating_point/capability-arbitrary-precision-floating-point.ll index 4489a44038..4391da3ec9 100644 --- a/test/extensions/INTEL/SPV_INTEL_arbitrary_precision_floating_point/capability-arbitrary-precision-floating-point.ll +++ b/test/extensions/INTEL/SPV_INTEL_arbitrary_precision_floating_point/capability-arbitrary-precision-floating-point.ll @@ -416,6 +416,11 @@ ; RUN: llvm-spirv -r %t.spv -o %t.r.bc ; RUN: llvm-dis < %t.r.bc | FileCheck %s --check-prefix=CHECK-LLVM +; Test with untyped pointers enabled. +; RUN: llvm-spirv %t.bc --spirv-ext=+SPV_INTEL_arbitrary_precision_integers,+SPV_INTEL_arbitrary_precision_floating_point,+SPV_KHR_untyped_pointers -o %t.spv +; RUN: llvm-spirv -r %t.spv -o %t.r.bc +; RUN: llvm-dis < %t.r.bc | FileCheck %s --check-prefix=CHECK-LLVM + ; CHECK-SPIRV: 2 Capability Kernel ; CHECK-SPIRV: 2 Capability ArbitraryPrecisionIntegersINTEL ; CHECK-SPIRV: 2 Capability ArbitraryPrecisionFloatingPointINTEL diff --git a/test/group_non_uniform_shuffle_down.ll b/test/group_non_uniform_shuffle_down.ll index b5822215f4..53dcb97aa5 100644 --- a/test/group_non_uniform_shuffle_down.ll +++ b/test/group_non_uniform_shuffle_down.ll @@ -1,16 +1,23 @@ ; RUN: llvm-as %s -o %t.bc -; RUN: llvm-spirv %t.bc -spirv-text -o - | FileCheck --check-prefix CHECK-SPIRV %s +; RUN: llvm-spirv %t.bc -spirv-text -o - | FileCheck --check-prefixes=CHECK-SPIRV,CHECK-SPIRV-TYPED-PTR %s ; RUN: llvm-spirv %t.bc -o %t.spv ; RUN: spirv-val %t.spv ; RUN: llvm-spirv -r %t.spv -o %t.rev.bc ; RUN: llvm-dis %t.rev.bc -o - | FileCheck --check-prefix CHECK-LLVM %s +; RUN: llvm-spirv %t.bc --spirv-ext=+SPV_KHR_untyped_pointers -spirv-text -o - | FileCheck --check-prefixes=CHECK-SPIRV,CHECK-SPIRV-UNTYPED-PTR %s +; RUN: llvm-spirv %t.bc --spirv-ext=+SPV_KHR_untyped_pointers -o %t.spv +; RUN: spirv-val %t.spv +; RUN: llvm-spirv -r %t.spv -o %t.rev.bc +; RUN: llvm-dis %t.rev.bc -o - | FileCheck --check-prefix CHECK-LLVM %s + ; CHECK-SPIRV-DAG: TypeInt [[#I32:]] 32 0 ; CHECK-SPIRV-DAG: Constant [[#I32]] [[#CONST_I32_3:]] 3 ; CHECK-SPIRV-DAG: Constant [[#I32]] [[#CONST_I32_8:]] 8 ; CHECK-SPIRV-DAG: TypeFloat [[#HALF:]] 16 ; CHECK-SPIRV-DAG: TypeStruct [[#S_HALF:]] [[#HALF]] -; CHECK-SPIRV-DAG: TypePointer [[#PTR_S_HALF:]] {{[0-9]+}} [[#S_HALF]] +; CHECK-SPIRV-TYPED-PTR-DAG: TypePointer [[#PTR_S_HALF:]] {{[0-9]+}} [[#S_HALF]] +; CHECK-SPIRV-UNTYPED-PTR-DAG: TypeUntypedPointerKHR [[#PTR:]] [[#]] target triple = "spir64-unknown-unknown" @@ -25,8 +32,10 @@ entry: ret void } -; CHECK-SPIRV: Variable {{[0-9]+}} {{[0-9]+}} -; CHECK-SPIRV: Variable [[#PTR_S_HALF]] [[#VAR_0:]] +; CHECK-SPIRV-TYPED-PTR: Variable {{[0-9]+}} {{[0-9]+}} +; CHECK-SPIRV-TYPED-PTR: Variable [[#PTR_S_HALF]] [[#VAR_0:]] +; CHECK-SPIRV-UNTYPED-PTR: UntypedVariableKHR {{[0-9]+}} {{[0-9]+}} +; CHECK-SPIRV-UNTYPED-PTR: UntypedVariableKHR [[#PTR]] [[#VAR_0:]] [[#HALF]] ; CHECK-SPIRV: Load [[#S_HALF]] [[#COMP_0:]] [[#VAR_0]] ; CHECK-SPIRV: CompositeExtract [[#HALF]] [[#ELEM_0:]] [[#COMP_0]] 0 ; CHECK-SPIRV: GroupNonUniformShuffleDown [[#HALF]] [[#ELEM_1:]] [[#CONST_I32_3]] [[#ELEM_0]] [[#CONST_I32_8]]