Skip to content

Commit 8b7ee37

Browse files
matborzyszkowskiigcbot
authored andcommitted
Adding support for opaque pointers in HandleSpirvDecorationMetadata
For `opaque pointers` we cannot deduce a type for `Prefetch` call with `ptr`. We need this information to create appropriate builtins. To achieve this, we can use `llvm::demangle()` method and find appropriate type. This change is compatible with both typed and opaque pointers.
1 parent cf6d8b2 commit 8b7ee37

File tree

3 files changed

+50
-9
lines changed

3 files changed

+50
-9
lines changed

IGC/AdaptorOCL/preprocess_spvir/HandleSPIRVDecorations/HandleSpirvDecorationMetadata.cpp

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ SPDX-License-Identifier: MIT
2222
#include <llvm/Support/Regex.h>
2323
#include "common/LLVMWarningsPop.hpp"
2424

25+
#include <regex>
26+
2527
using namespace llvm;
2628
using namespace IGC;
2729

@@ -40,6 +42,46 @@ HandleSpirvDecorationMetadata::HandleSpirvDecorationMetadata() : ModulePass(ID)
4042
initializeHandleSpirvDecorationMetadataPass(*PassRegistry::getPassRegistry());
4143
}
4244

45+
// Get type from demangled name for both typed and opaque pointers
46+
// For opaque pointers, we cannot deduce type from ptr
47+
Type *HandleSpirvDecorationMetadata::getArgumentType(std::string demangledName, size_t argNo, llvm::LLVMContext &ctx) {
48+
auto open = demangledName.find('('), close = demangledName.find(')', open);
49+
if (open == std::string::npos || close == std::string::npos || close <= open + 1) {
50+
IGC_ASSERT_MESSAGE(0, "Unexpected demangled name!");
51+
return nullptr;
52+
}
53+
54+
std::stringstream ss(demangledName.substr(open + 1, close - open - 1));
55+
std::string arg;
56+
std::vector<std::string> args;
57+
58+
while (std::getline(ss, arg, ',')) {
59+
arg.erase(0, arg.find_first_not_of(" \t"));
60+
arg.erase(arg.find_last_not_of(" \t") + 1);
61+
args.push_back(arg);
62+
}
63+
64+
if (argNo >= args.size()) {
65+
IGC_ASSERT_MESSAGE(0, "Provided arg number is greater than or equal to arg.size()!");
66+
return nullptr;
67+
}
68+
69+
const auto &a = args[argNo];
70+
71+
if (std::regex_search(a, std::regex("\\bchar\\b")))
72+
return llvm::Type::getInt8Ty(ctx);
73+
else if (std::regex_search(a, std::regex("\\bshort\\b")))
74+
return llvm::Type::getInt16Ty(ctx);
75+
else if (std::regex_search(a, std::regex("\\bint\\b")))
76+
return llvm::Type::getInt32Ty(ctx);
77+
else if (std::regex_search(a, std::regex("\\blong\\b")))
78+
return llvm::Type::getInt64Ty(ctx);
79+
else
80+
IGC_ASSERT_MESSAGE(0, "Unexpected type!");
81+
82+
return nullptr;
83+
}
84+
4385
bool HandleSpirvDecorationMetadata::runOnModule(Module &module) {
4486
m_Metadata = getAnalysis<MetaDataUtilsWrapper>().getModuleMetaData();
4587
m_pCtx = getAnalysis<CodeGenContextWrapper>().getCodeGenContext();
@@ -388,7 +430,7 @@ void HandleSpirvDecorationMetadata::handleCacheControlINTELFor1DBlockIO(CallInst
388430
std::string funcName;
389431
if constexpr (std::is_same_v<T, LoadCacheControl>) {
390432
if (auto isPrefetch = I.getType()->isVoidTy()) {
391-
operationType = IGCLLVM::getNonOpaquePtrEltTy(I.getArgOperand(0)->getType());
433+
operationType = getArgumentType(llvm::demangle(F->getName().str()), 0, F->getContext());
392434
funcName = "SubgroupBlockPrefetchINTEL";
393435
} else {
394436
operationType = I.getType();

IGC/AdaptorOCL/preprocess_spvir/HandleSPIRVDecorations/HandleSpirvDecorationMetadata.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,7 @@ class HandleSpirvDecorationMetadata : public llvm::ModulePass, public llvm::Inst
7070
void handleCacheControlINTELFor1DBlockIO(llvm::CallInst &I, llvm::SmallPtrSetImpl<llvm::MDNode *> &MDNodes);
7171
void handleCacheControlINTELForOCL1DBlockPrefetch(llvm::CallInst &I, llvm::SmallPtrSetImpl<llvm::MDNode *> &MDNodes,
7272
llvm::SmallVectorImpl<llvm::StringRef> &Matches);
73+
74+
llvm::Type *getArgumentType(std::string demangledName, size_t argNo, llvm::LLVMContext &ctx);
7375
};
7476
} // namespace IGC

IGC/ocloc_tests/features/cache_controls/cl_intel_subgroups_buffer_prefetch/SPV_INTEL_subgroup_buffer_prefetch.ll

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,18 @@
77
;============================ end_copyright_notice =============================
88

99
; REQUIRES: llvm-spirv, regkeys, pvc-supported, llvm-15-or-older
10-
11-
; TODO: This test fails on LLVM 16 with opaque pointers, but passes with typed pointers. Once the necessary fixes are made, remove llvm-15-or-older from the list above. llvm-15-or-older was added to the list above to prevent non-zero return from llvm-lit on LLVM 16 build with typed pointers forced.
12-
; XFAIL: llvm-16-plus
1310
; UNSUPPORTED: sys32
1411

1512
; LLVM with opaque pointers:
16-
; TODO: llvm-as -opaque-pointers=1 %s -o %t.bc
13+
; RUN: llvm-as -opaque-pointers=1 %s -o %t.bc
1714
; COM: Replace SPV_INTEL_subgroups with SPV_INTEL_subgroup_buffer_prefetch once support for prefetches is implemented in the KHR Translator
18-
; TODO: llvm-spirv %t.bc -opaque-pointers=1 --spirv-ext=+SPV_INTEL_subgroups,+SPV_INTEL_cache_controls -o %t.spv
19-
; TODO: ocloc compile -spirv_input -file %t.spv -device pvc -options " -igc_opts 'EnableOpaquePointersBackend=1,DumpVISAASMToConsole=1'" 2>&1 | FileCheck %s
15+
; RUN: llvm-spirv %t.bc -opaque-pointers=1 --spirv-ext=+SPV_INTEL_subgroups,+SPV_INTEL_cache_controls -o %t.spv
16+
; RUN: ocloc compile -spirv_input -file %t.spv -device pvc -options " -igc_opts 'EnableOpaquePointersBackend=1, DumpVISAASMToConsole=1'" 2>&1 | FileCheck %s
2017

2118
; LLVM with typed pointers/default pointer typing:
22-
; RUN: llvm-as %s -o %t.bc
19+
; RUN: llvm-as -opaque-pointers=0 %s -o %t.bc
2320
; COM: Replace SPV_INTEL_subgroups with SPV_INTEL_subgroup_buffer_prefetch once support for prefetches is implemented in the KHR Translator
24-
; RUN: llvm-spirv %t.bc --spirv-ext=+SPV_INTEL_subgroups,+SPV_INTEL_cache_controls -o %t.spv
21+
; RUN: llvm-spirv %t.bc -opaque-pointers=0 --spirv-ext=+SPV_INTEL_subgroups,+SPV_INTEL_cache_controls -o %t.spv
2522
; RUN: ocloc compile -spirv_input -file %t.spv -device pvc -options " -igc_opts 'DumpVISAASMToConsole=1'" 2>&1 | FileCheck %s
2623

2724
target triple = "spir64-unknown-unknown"

0 commit comments

Comments
 (0)