From 196464d143c10b14fc721ed4df8ed448bc6452f3 Mon Sep 17 00:00:00 2001 From: "Sidorov, Dmitry" Date: Mon, 28 Aug 2023 04:44:51 -0700 Subject: [PATCH] Address comments and fix uadd Signed-off-by: Sidorov, Dmitry --- include/LLVMSPIRVOpts.h | 10 +++++----- lib/SPIRV/SPIRVWriter.cpp | 8 ++++---- lib/SPIRV/libSPIRV/SPIRVModule.h | 4 ++-- test/llvm-intrinsics/add_sub.sat.ll | 8 +++----- tools/llvm-spirv/llvm-spirv.cpp | 13 +++++++------ 5 files changed, 21 insertions(+), 22 deletions(-) diff --git a/include/LLVMSPIRVOpts.h b/include/LLVMSPIRVOpts.h index 4b96679b7a..1901c0306d 100644 --- a/include/LLVMSPIRVOpts.h +++ b/include/LLVMSPIRVOpts.h @@ -193,12 +193,12 @@ class TranslatorOpts { ReplaceLLVMFmulAddWithOpenCLMad = Value; } - void setUseOpenCLExtInstructionsForLLVMIntrinsic(bool Value) noexcept { - UseOpenCLExtInstructionsForLLVMIntrinsic = Value; + void setUseOpenCLExtInstructionsForLLVMMathIntrinsic(bool Value) noexcept { + UseOpenCLExtInstructionsForLLVMMathIntrinsic = Value; } - bool shouldUseOpenCLExtInstructionsForLLVMIntrinsic() const noexcept { - return UseOpenCLExtInstructionsForLLVMIntrinsic; + bool shouldUseOpenCLExtInstructionsForLLVMMathIntrinsic() const noexcept { + return UseOpenCLExtInstructionsForLLVMMathIntrinsic; } bool shouldPreserveOCLKernelArgTypeMetadataThroughString() const noexcept { @@ -254,7 +254,7 @@ class TranslatorOpts { // Controls whether llvm math intrinsics should be replaced with instructions // from OpenCL extended instruction set or emulated by native SPIR-V // instructions - bool UseOpenCLExtInstructionsForLLVMIntrinsic = true; + bool UseOpenCLExtInstructionsForLLVMMathIntrinsic = true; // Add a workaround to preserve OpenCL kernel_arg_type and // kernel_arg_type_qual metadata through OpString diff --git a/lib/SPIRV/SPIRVWriter.cpp b/lib/SPIRV/SPIRVWriter.cpp index cf2424b92c..744174d50d 100644 --- a/lib/SPIRV/SPIRVWriter.cpp +++ b/lib/SPIRV/SPIRVWriter.cpp @@ -3860,7 +3860,7 @@ SPIRVValue *LLVMToSPIRVBase::transIntrinsicInst(IntrinsicInst *II, case Intrinsic::usub_sat: case Intrinsic::sadd_sat: case Intrinsic::ssub_sat: { - if (BM->shouldUseOpenCLExtInstructionsForLLVMIntrinsic()) { + if (BM->shouldUseOpenCLExtInstructionsForLLVMMathIntrinsic()) { SPIRVWord ExtOp; if (IID == Intrinsic::uadd_sat) ExtOp = OpenCLLIB::UAdd_sat; @@ -3889,13 +3889,13 @@ SPIRVValue *LLVMToSPIRVBase::transIntrinsicInst(IntrinsicInst *II, SPIRVValue *SecondArgVal = transValue(II->getArgOperand(1), BB); if (IID == Intrinsic::uadd_sat) { - // uadd.sat(a, b) -> res = a + b, (res > a) ? res : MAX + // uadd.sat(a, b) -> res = a + b, (res >= a) ? res : MAX SPIRVValue *Max = transValue(Constant::getAllOnesValue(II->getType()), BB); SPIRVValue *Add = BM->addBinaryInst(OpIAdd, Ty, FirstArgVal, SecondArgVal, BB); SPIRVValue *Cmp = - BM->addCmpInst(OpUGreaterThan, SPVBoolTy, Add, FirstArgVal, BB); + BM->addCmpInst(OpUGreaterThanEqual, SPVBoolTy, Add, FirstArgVal, BB); return BM->addSelectInst(Cmp, Add, Max, BB); } if (IID == Intrinsic::usub_sat) { @@ -3931,7 +3931,7 @@ SPIRVValue *LLVMToSPIRVBase::transIntrinsicInst(IntrinsicInst *II, // The later was chosed because of the following consideration: // speculative branch prediction will most likely consider 'if' statements // here as always false falling through to 'a + b', and reversing it will - // case performance degradation. + // cause performance degradation. SPIRVValue *Add = BM->addBinaryInst(OpIAdd, Ty, FirstArgVal, SecondArgVal, BB); diff --git a/lib/SPIRV/libSPIRV/SPIRVModule.h b/lib/SPIRV/libSPIRV/SPIRVModule.h index abaa5726bd..8ddbdcfc7d 100644 --- a/lib/SPIRV/libSPIRV/SPIRVModule.h +++ b/lib/SPIRV/libSPIRV/SPIRVModule.h @@ -534,8 +534,8 @@ class SPIRVModule { return TranslationOpts.shouldReplaceLLVMFmulAddWithOpenCLMad(); } - bool shouldUseOpenCLExtInstructionsForLLVMIntrinsic() const noexcept { - return TranslationOpts.shouldUseOpenCLExtInstructionsForLLVMIntrinsic(); + bool shouldUseOpenCLExtInstructionsForLLVMMathIntrinsic() const noexcept { + return TranslationOpts.shouldUseOpenCLExtInstructionsForLLVMMathIntrinsic(); } bool shouldPreserveOCLKernelArgTypeMetadataThroughString() const noexcept { diff --git a/test/llvm-intrinsics/add_sub.sat.ll b/test/llvm-intrinsics/add_sub.sat.ll index 585e0b3476..ce518aa5d7 100644 --- a/test/llvm-intrinsics/add_sub.sat.ll +++ b/test/llvm-intrinsics/add_sub.sat.ll @@ -3,17 +3,15 @@ ; RUN: llvm-spirv %t.spv -to-text -o - | FileCheck %s --check-prefixes=COMMON,OPENCL ; RUN: spirv-val %t.spv -; RUN: llvm-as < %s -o %t.bc -; RUN: llvm-spirv %t.bc --spirv-use-ocl-math-for-llvm-intrinsic=true -o %t.spv +; RUN: llvm-spirv %t.bc --spirv-use-ocl-for-llvm-math-intrinsic=true -o %t.spv ; RUN: llvm-spirv %t.spv -to-text -o - | FileCheck %s --check-prefixes=COMMON,OPENCL ; RUN: spirv-val %t.spv -; RUN: llvm-as < %s -o %t.bc -; RUN: llvm-spirv %t.bc --spirv-use-ocl-math-for-llvm-intrinsic=false -o %t.spv +; RUN: llvm-spirv %t.bc --spirv-use-ocl-for-llvm-math-intrinsic=false -o %t.spv ; RUN: llvm-spirv %t.spv -to-text -o - | FileCheck %s --check-prefixes=COMMON,EMULATION ; RUN: spirv-val %t.spv -; Test checks that saturation addition and substraction llvm intrinsics +; Test checks that saturation addition and subtraction llvm intrinsics ; are translated into instruction from OpenCL Extended Instruction Set. target datalayout = "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024" diff --git a/tools/llvm-spirv/llvm-spirv.cpp b/tools/llvm-spirv/llvm-spirv.cpp index 5c89bbcfeb..f358390bb1 100644 --- a/tools/llvm-spirv/llvm-spirv.cpp +++ b/tools/llvm-spirv/llvm-spirv.cpp @@ -258,8 +258,8 @@ static cl::opt SPIRVBuiltinFormat( clEnumValN(SPIRV::BuiltinFormat::Global, "global", "Use globals to represent SPIR-V builtin variables"))); -static cl::opt SPIRVUseOpenCLExtInstructionsForLLVMIntrinsic( - "spirv-use-ocl-math-for-llvm-intrinsic", cl::init(true), +static cl::opt SPIRVUseOpenCLExtInstructionsForLLVMMathIntrinsic( + "spirv-use-ocl-for-llvm-math-intrinsic", cl::init(true), cl::desc("Allow to use OpenCL.ExtendedInstructionSet.100 to translate " "LLVM math intrinsics. Otherwise use emulation for these " "intrinsics)")); @@ -763,13 +763,14 @@ int main(int Ac, char **Av) { } } - if (SPIRVUseOpenCLExtInstructionsForLLVMIntrinsic.getNumOccurrences() != 0) { + if (SPIRVUseOpenCLExtInstructionsForLLVMMathIntrinsic.getNumOccurrences() != + 0) { if (IsReverse) { - errs() << "Note: --spirv-use-ocl-math-for-llvm-intrinsic option ignored " + errs() << "Note: --spirv-use-ocl-for-llvm-math-intrinsic option ignored " "as it only affects translation from LLVM IR to SPIR-V"; } else { - Opts.setUseOpenCLExtInstructionsForLLVMIntrinsic( - SPIRVUseOpenCLExtInstructionsForLLVMIntrinsic); + Opts.setUseOpenCLExtInstructionsForLLVMMathIntrinsic( + SPIRVUseOpenCLExtInstructionsForLLVMMathIntrinsic); } }