Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions llvm/lib/Target/SPIRV/SPIRVPrepareFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ class SPIRVPrepareFunctions : public ModulePass {
}
};

static cl::list<std::string> SPVAllowUnknownIntrinsics(
"spv-allow-unknown-intrinsics", cl::CommaSeparated,
cl::desc("Emit unknown intrinsics as calls to external functions. A "
"comma-separated input list of intrinsic prefixes must be "
"provided, and only intrinsics carrying a listed prefix get "
"emitted as described."),
cl::value_desc("intrinsic_prefix_0,intrinsic_prefix_1"), cl::ValueOptional);
} // namespace

char SPIRVPrepareFunctions::ID = 0;
Expand Down Expand Up @@ -445,6 +452,15 @@ bool SPIRVPrepareFunctions::substituteIntrinsicCalls(Function *F) {
EraseFromParent);
Changed = true;
break;
default:
if (TM.getTargetTriple().getVendor() == Triple::AMD ||
any_of(SPVAllowUnknownIntrinsics, [II](auto &&Prefix) {
if (Prefix.empty())
return false;
return II->getCalledFunction()->getName().starts_with(Prefix);
}))
Changed |= lowerIntrinsicToFunction(II);
break;
}
}
}
Expand Down
36 changes: 36 additions & 0 deletions llvm/test/CodeGen/SPIRV/allow_unknown_intrinsics.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
; RUN: not llc -verify-machineinstrs -O0 -mtriple=spirv64-unknown-unknown %s -o %t.spvt 2>&1 | FileCheck -check-prefix=CHECK-ERROR %s
; RUN: not llc -verify-machineinstrs -O0 -mtriple=spirv64-unknown-unknown --spv-allow-unknown-intrinsics %s -o %t.spvt 2>&1 | FileCheck -check-prefix=CHECK-ERROR %s
; RUN: not llc -verify-machineinstrs -O0 -mtriple=spirv64-unknown-unknown --spv-allow-unknown-intrinsics=notllvm %s -o %t.spvt 2>&1 | FileCheck --check-prefix=CHECK-ERROR %s
; RUN: not llc -verify-machineinstrs -O0 -mtriple=spirv64-unknown-unknown --spv-allow-unknown-intrinsics=llvm.some.custom %s -o %t.spvt 2>&1 | FileCheck --check-prefix=CHECK-ERROR %s
; RUN: llc -verify-machineinstrs -O0 -mtriple=spirv64-unknown-unknown --spv-allow-unknown-intrinsics=llvm. %s -o - | FileCheck %s
; RUN: llc -verify-machineinstrs -O0 -mtriple=spirv64-unknown-unknown --spv-allow-unknown-intrinsics=llvm.,random.prefix %s -o - | FileCheck %s
; RUN: llc -verify-machineinstrs -O0 -mtriple=spirv64-amd-amdhsa %s -o - | FileCheck %s
; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv64-unknown-unknown --spv-allow-unknown-intrinsics=llvm. %s -o - -filetype=obj | spirv-val %}
; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv64-amd-amdhsa %s -o - -filetype=obj | spirv-val %}

; The test checks command-line option which allows to represent unknown
; intrinsics as external function calls in SPIR-V.

; CHECK-ERROR: LLVM ERROR: unable to legalize instruction: %3:iid(s64) = G_READCYCLECOUNTER (in function: foo)

; CHECK: Name %[[READCYCLECOUNTER:[0-9]+]] "spirv.llvm_readcyclecounter"
; CHECK: Name %[[SOME_CUSTOM_INTRINSIC:[0-9]+]] "spirv.llvm_some_custom_intrinsic"
; CHECK-DAG: Decorate %[[READCYCLECOUNTER]] LinkageAttributes {{.*}} Import
; CHECK: Decorate %[[SOME_CUSTOM_INTRINSIC]] LinkageAttributes {{.*}} Import
; CHECK-DAG: %[[I64:[0-9]+]] = OpTypeInt 64
; CHECK: %[[FnTy:[0-9]+]] = OpTypeFunction %[[I64]]
; CHECK: %[[READCYCLECOUNTER]] = OpFunction %[[I64]] {{.*}} %[[FnTy]]
; CHECK-DAG: %[[SOME_CUSTOM_INTRINSIC]] = OpFunction %[[I64]] {{.*}} %[[FnTy]]
; CHECK-DAG: OpFunctionCall %[[I64]] %[[READCYCLECOUNTER]]
; CHECK: OpFunctionCall %[[I64]] %[[SOME_CUSTOM_INTRINSIC]]

define spir_func void @foo() {
entry:
; TODO: if and when the SPIR-V learns how to lower readcyclecounter, we will have to pick another unhandled intrinsic
%0 = call i64 @llvm.readcyclecounter()
%1 = call i64 @llvm.some.custom.intrinsic()
ret void
}

declare i64 @llvm.readcyclecounter()
declare i64 @llvm.some.custom.intrinsic()