Skip to content

Commit

Permalink
Address comments and fix uadd
Browse files Browse the repository at this point in the history
Signed-off-by: Sidorov, Dmitry <dmitry.sidorov@intel.com>
  • Loading branch information
MrSidims committed Aug 28, 2023
1 parent 56bfd0b commit 196464d
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 22 deletions.
10 changes: 5 additions & 5 deletions include/LLVMSPIRVOpts.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions lib/SPIRV/SPIRVWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);

Expand Down
4 changes: 2 additions & 2 deletions lib/SPIRV/libSPIRV/SPIRVModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 3 additions & 5 deletions test/llvm-intrinsics/add_sub.sat.ll
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
13 changes: 7 additions & 6 deletions tools/llvm-spirv/llvm-spirv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,8 @@ static cl::opt<SPIRV::BuiltinFormat> SPIRVBuiltinFormat(
clEnumValN(SPIRV::BuiltinFormat::Global, "global",
"Use globals to represent SPIR-V builtin variables")));

static cl::opt<bool> SPIRVUseOpenCLExtInstructionsForLLVMIntrinsic(
"spirv-use-ocl-math-for-llvm-intrinsic", cl::init(true),
static cl::opt<bool> 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)"));
Expand Down Expand Up @@ -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);
}
}

Expand Down

0 comments on commit 196464d

Please sign in to comment.