Skip to content

Commit 6e66432

Browse files
igorban-inteligcbot
authored andcommitted
Casting pointers for BiFs mnemonic
.
1 parent 4db165d commit 6e66432

File tree

3 files changed

+137
-4
lines changed

3 files changed

+137
-4
lines changed

IGC/VectorCompiler/lib/GenXOpts/CMTrans/GenXTranslateSPIRVBuiltins.cpp

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ SPDX-License-Identifier: MIT
2222
#include "vc/Support/GenXDiagnostic.h"
2323
#include "vc/Utils/GenX/IntrinsicsWrapper.h"
2424
#include "vc/Utils/General/BiF.h"
25+
#include "vc/Utils/General/Types.h"
2526

2627
#include "Probe/Assertion.h"
2728

@@ -382,6 +383,62 @@ static bool isSPIRVBuiltinDecl(const Function &F) {
382383
return Name.contains("__spirv");
383384
}
384385

386+
static void emitError(Type *ArgTy, Type *NewArgTy, unsigned Index,
387+
LLVMContext &Ctx, CallInst *CI) {
388+
SmallString<128> Message;
389+
raw_svector_ostream Out(Message);
390+
Out << "Unexpected function argument #" << Index << " type: " << *ArgTy
391+
<< ", expected: " << *NewArgTy << "\n";
392+
vc::diagnose(Ctx, "GenXTranslateSPIRVBuiltins", Message, CI);
393+
}
394+
395+
static inline void checkTypesFixPtrs(Function *Func, Function *NewFunc) {
396+
SmallVector<CallInst *, 1> CallInstList;
397+
398+
// If types not matched - we try to modify it by cast's
399+
if (Func->getFunctionType() != NewFunc->getFunctionType()) {
400+
for (auto *U : Func->users()) {
401+
auto *CI = dyn_cast<CallInst>(U);
402+
if (!CI)
403+
continue;
404+
405+
CallInstList.push_back(CI);
406+
IRBuilder<> Builder(CI);
407+
408+
for (auto &U : CI->args()) {
409+
auto Index = U.getOperandNo();
410+
auto *ArgTy = U->getType();
411+
auto *NewArgTy = NewFunc->getArg(Index)->getType();
412+
413+
if (isa<PointerType>(ArgTy) && isa<PointerType>(NewArgTy)) {
414+
auto AS = cast<PointerType>(ArgTy)->getAddressSpace();
415+
auto NewAS = cast<PointerType>(NewArgTy)->getAddressSpace();
416+
if (AS != NewAS && NewAS != vc::AddrSpace::Generic)
417+
emitError(ArgTy, NewArgTy, Index, CI->getContext(), CI);
418+
419+
U.set(Builder.CreatePointerBitCastOrAddrSpaceCast(U.get(), NewArgTy));
420+
} else if (ArgTy != NewArgTy)
421+
emitError(ArgTy, NewArgTy, Index, CI->getContext(), CI);
422+
}
423+
}
424+
}
425+
Func->deleteBody();
426+
427+
if (!CallInstList.empty()) {
428+
Func->stealArgumentListFrom(*NewFunc);
429+
// A new function is needed to replase FunctionType in all calls
430+
auto *CastFunc =
431+
Function::Create(NewFunc->getFunctionType(), Func->getLinkage(),
432+
NewFunc->getName(), Func->getParent());
433+
CastFunc->copyAttributesFrom(Func);
434+
for (auto *CI : CallInstList)
435+
CI->setCalledFunction(CastFunc);
436+
437+
Func->eraseFromParent();
438+
CastFunc->setName(NewFunc->getName());
439+
}
440+
}
441+
385442
bool GenXTranslateSPIRVBuiltins::runOnModule(Module &M) {
386443
bool Changed = false;
387444
Expander = SPIRVExpander(&M);
@@ -404,10 +461,8 @@ bool GenXTranslateSPIRVBuiltins::runOnModule(Module &M) {
404461
for (auto &FuncName : SPIRVBuiltins) {
405462
auto *Func = M.getFunction(FuncName);
406463
auto *NewFunc = SPIRVBuiltinsModule->getFunction(FuncName);
407-
if (Func && !Func->isDeclaration() && NewFunc &&
408-
!NewFunc->isDeclaration() &&
409-
Func->getFunctionType() == NewFunc->getFunctionType())
410-
Func->deleteBody();
464+
if (Func && !Func->isDeclaration() && NewFunc && !NewFunc->isDeclaration())
465+
checkTypesFixPtrs(Func, NewFunc);
411466
}
412467

413468
if (Linker::linkModules(M, std::move(SPIRVBuiltinsModule),
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
;=========================== begin_copyright_notice ============================
2+
;
3+
; Copyright (C) 2023 Intel Corporation
4+
;
5+
; SPDX-License-Identifier: MIT
6+
;
7+
;============================ end_copyright_notice =============================
8+
;
9+
; RUN: %opt %use_old_pass_manager% -GenXTranslateSPIRVBuiltins \
10+
; RUN: -vc-spirv-builtins-bif-path=%VC_SPIRV_OCL_BIF% -march=genx64 \
11+
; RUN: -mtriple=spir64-unknown-unknown -mcpu=XeHPC -S < %s | FileCheck %s
12+
; ------------------------------------------------
13+
; GenXTranslateSPIRVBuiltins
14+
; ------------------------------------------------
15+
16+
; Function Attrs: mustprogress nofree nosync nounwind readnone willreturn
17+
declare spir_func i64 @_Z33__spirv_BuiltInGlobalInvocationIdi(i32) #2
18+
19+
; Function Attrs: noinline nounwind
20+
; CHECK-LABEL: void @__assert_fail
21+
define spir_func void @__assert_fail([31 x i8] addrspace(2)* %0, [11 x i8] addrspace(2)* %1, i32 %2, [56 x i8] addrspace(2)* %3) {
22+
%5 = call spir_func i64 @_Z33__spirv_BuiltInGlobalInvocationIdi(i32 0) #3
23+
; Multi-call must be handled
24+
; CHECK: [[ARG1:%[^ ]*]] = addrspacecast [31 x i8] addrspace(2)* %0 to i8 addrspace(4)*
25+
; CHECK: [[ARG2:%[^ ]*]] = addrspacecast [11 x i8] addrspace(2)* %1 to i8 addrspace(4)*
26+
; CHECK: [[ARG3:%[^ ]*]] = addrspacecast [56 x i8] addrspace(2)* %3 to i8 addrspace(4)*
27+
; CHECK: call spir_func void @__devicelib_assert_fail(i8 addrspace(4)* [[ARG1]], i8 addrspace(4)* [[ARG2]], i32 %2, i8 addrspace(4)* [[ARG3]]
28+
call spir_func void @__devicelib_assert_fail([31 x i8] addrspace(2)* %0, [11 x i8] addrspace(2)* %1, i32 %2, [56 x i8] addrspace(2)* %3, i64 %5, i64 %5, i64 %5, i64 %5, i64 %5, i64 %5) #1
29+
; CHECK: void @__devicelib_assert_fail(i8 addrspace(4)*{{.*}}, i8 addrspace(4)*{{.*}}, i32{{.*}}, i8 addrspace(4)*
30+
call spir_func void @__devicelib_assert_fail([31 x i8] addrspace(2)* %0, [11 x i8] addrspace(2)* %1, i32 %2, [56 x i8] addrspace(2)* %3, i64 %5, i64 %5, i64 %5, i64 %5, i64 %5, i64 %5) #1
31+
ret void
32+
}
33+
34+
; Function Attrs: noinline nounwind
35+
; CHECK-LABEL: define {{.*}} void @__devicelib_assert_fail
36+
define spir_func void @__devicelib_assert_fail([31 x i8] addrspace(2)* %0, [11 x i8] addrspace(2)* %1, i32 %2, [56 x i8] addrspace(2)* %3, i64 %4, i64 %5, i64 %6, i64 %7, i64 %8, i64 %9) #1 {
37+
; CHECK: tail call void @llvm.debugtrap()
38+
; CHECK: tail call void @llvm.trap()
39+
ret void
40+
}
41+
42+
attributes #2 = { mustprogress nofree nosync nounwind readnone willreturn }
43+
attributes #3 = { nounwind readnone willreturn }
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
;=========================== begin_copyright_notice ============================
2+
;
3+
; Copyright (C) 2023 Intel Corporation
4+
;
5+
; SPDX-License-Identifier: MIT
6+
;
7+
;============================ end_copyright_notice =============================
8+
;
9+
; RUN: not %opt %use_old_pass_manager% -GenXTranslateSPIRVBuiltins \
10+
; RUN: -vc-spirv-builtins-bif-path=%VC_SPIRV_OCL_BIF% -march=genx64 \
11+
; RUN: -mtriple=spir64-unknown-unknown -mcpu=XeHPC -S %s 2>&1 | FileCheck %s
12+
; ------------------------------------------------
13+
; GenXTranslateSPIRVBuiltins
14+
; ------------------------------------------------
15+
16+
; Function Attrs: mustprogress nofree nosync nounwind readnone willreturn
17+
declare spir_func i64 @_Z33__spirv_BuiltInGlobalInvocationIdi(i32) #2
18+
19+
; CHECK: LLVM ERROR: GenXTranslateSPIRVBuiltins failed for: < call spir_func void @__devicelib_assert_fail
20+
; CHECK: Unexpected function argument #9 type: i32, expected: i64
21+
22+
; Function Attrs: noinline nounwind
23+
define spir_func void @__assert_fail(i16 addrspace(4)* %0, [11 x i8] addrspace(4)* %1, i32 %2, [56 x i8] addrspace(4)* %3) {
24+
%5 = call spir_func i64 @_Z33__spirv_BuiltInGlobalInvocationIdi(i32 0) #3
25+
call spir_func void @__devicelib_assert_fail(i16 addrspace(4)* %0, [11 x i8] addrspace(4)* %1, i32 %2, [56 x i8] addrspace(4)* %3, i64 %5, i64 %5, i64 %5, i64 %5, i64 %5, i32 %2) #1
26+
ret void
27+
}
28+
29+
; Function Attrs: noinline nounwind
30+
define spir_func void @__devicelib_assert_fail(i16 addrspace(4)* %0, [11 x i8] addrspace(4)* %1, i32 %2, [56 x i8] addrspace(4)* %3, i64 %4, i64 %5, i64 %6, i64 %7, i64 %8, i32 %9) #1 {
31+
ret void
32+
}
33+
34+
attributes #2 = { mustprogress nofree nosync nounwind readnone willreturn }
35+
attributes #3 = { nounwind readnone willreturn }

0 commit comments

Comments
 (0)