Skip to content

Commit dbd00a5

Browse files
[SPIRV] Improve type inference of operand presented by opaque pointers and aggregate types (#98035)
This PR improves type inference of operand presented by opaque pointers and aggregate types: * tries to restore original function return type for aggregate types so that it's possible to deduce a correct type during emit-intrinsics step (see llvm/test/CodeGen/SPIRV/SpecConstants/restore-spec-type.ll for the reproducer of the previously existed issue when spirv-val found a mismatch between object and ptr types in OpStore due to the incorrect aggregate types tracing), * explores untyped pointer operands of store to deduce correct pointee types, * creates an extension type to track pointee types from emit-intrinsics step and further instead of direct and naive usage of TypePointerType that led previously to crashes due to ban of creation of Value of TypePointerType type, * tracks instructions with uncomplete type information and tries to improve their type info after pass calculated types for all machine functions (it doesn't traverse a code but rather checks only those instructions which were tracked as uncompleted), * address more cases of removing unnecessary bitcasts (see, for example, changes in test/CodeGen/SPIRV/transcoding/OpGenericCastToPtr.ll where `CHECK-SPIRV-NEXT` in LIT checks show absence of unneeded bitcasts and unmangled/mangled versions have proper typing now with equivalent type info), * address more cases of well known types or relations between types within instructions (see, for example, atomic*.ll test cases and Event-related test cases for improved SPIR-V code generated by the Backend), * fix the issue of removing unneeded ptrcast instructions in pre-legalizer pass that led to creation of new assign-type instructions with the same argument as source in ptrcast and caused errors in type inference (the reproducer `complex.ll` test case is added to the PR).
1 parent 4710e0f commit dbd00a5

20 files changed

+871
-256
lines changed

llvm/lib/Target/SPIRV/SPIRVBuiltins.cpp

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -169,21 +169,9 @@ using namespace InstructionSet;
169169
// TableGen records
170170
//===----------------------------------------------------------------------===//
171171

172-
/// Looks up the demangled builtin call in the SPIRVBuiltins.td records using
173-
/// the provided \p DemangledCall and specified \p Set.
174-
///
175-
/// The lookup follows the following algorithm, returning the first successful
176-
/// match:
177-
/// 1. Search with the plain demangled name (expecting a 1:1 match).
178-
/// 2. Search with the prefix before or suffix after the demangled name
179-
/// signyfying the type of the first argument.
180-
///
181-
/// \returns Wrapper around the demangled call and found builtin definition.
182-
static std::unique_ptr<const SPIRV::IncomingCall>
183-
lookupBuiltin(StringRef DemangledCall,
184-
SPIRV::InstructionSet::InstructionSet Set,
185-
Register ReturnRegister, const SPIRVType *ReturnType,
186-
const SmallVectorImpl<Register> &Arguments) {
172+
namespace SPIRV {
173+
/// Parses the name part of the demangled builtin call.
174+
std::string lookupBuiltinNameHelper(StringRef DemangledCall) {
187175
const static std::string PassPrefix = "(anonymous namespace)::";
188176
std::string BuiltinName;
189177
// Itanium Demangler result may have "(anonymous namespace)::" prefix
@@ -215,6 +203,27 @@ lookupBuiltin(StringRef DemangledCall,
215203
BuiltinName = BuiltinName.substr(0, BuiltinName.find("_R"));
216204
}
217205

206+
return BuiltinName;
207+
}
208+
} // namespace SPIRV
209+
210+
/// Looks up the demangled builtin call in the SPIRVBuiltins.td records using
211+
/// the provided \p DemangledCall and specified \p Set.
212+
///
213+
/// The lookup follows the following algorithm, returning the first successful
214+
/// match:
215+
/// 1. Search with the plain demangled name (expecting a 1:1 match).
216+
/// 2. Search with the prefix before or suffix after the demangled name
217+
/// signyfying the type of the first argument.
218+
///
219+
/// \returns Wrapper around the demangled call and found builtin definition.
220+
static std::unique_ptr<const SPIRV::IncomingCall>
221+
lookupBuiltin(StringRef DemangledCall,
222+
SPIRV::InstructionSet::InstructionSet Set,
223+
Register ReturnRegister, const SPIRVType *ReturnType,
224+
const SmallVectorImpl<Register> &Arguments) {
225+
std::string BuiltinName = SPIRV::lookupBuiltinNameHelper(DemangledCall);
226+
218227
SmallVector<StringRef, 10> BuiltinArgumentTypes;
219228
StringRef BuiltinArgs =
220229
DemangledCall.slice(DemangledCall.find('(') + 1, DemangledCall.find(')'));
@@ -2610,9 +2619,6 @@ Type *parseBuiltinCallArgumentBaseType(const StringRef DemangledCall,
26102619
// Unable to recognize SPIRV type name.
26112620
return nullptr;
26122621

2613-
if (BaseType->isVoidTy())
2614-
BaseType = Type::getInt8Ty(Ctx);
2615-
26162622
// Handle "typeN*" or "type vector[N]*".
26172623
TypeStr.consume_back("*");
26182624

@@ -2621,7 +2627,8 @@ Type *parseBuiltinCallArgumentBaseType(const StringRef DemangledCall,
26212627

26222628
TypeStr.getAsInteger(10, VecElts);
26232629
if (VecElts > 0)
2624-
BaseType = VectorType::get(BaseType, VecElts, false);
2630+
BaseType = VectorType::get(
2631+
BaseType->isVoidTy() ? Type::getInt8Ty(Ctx) : BaseType, VecElts, false);
26252632

26262633
return BaseType;
26272634
}

llvm/lib/Target/SPIRV/SPIRVBuiltins.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
namespace llvm {
2121
namespace SPIRV {
22+
/// Parses the name part of the demangled builtin call.
23+
std::string lookupBuiltinNameHelper(StringRef DemangledCall);
2224
/// Lowers a builtin function call using the provided \p DemangledCall skeleton
2325
/// and external instruction \p Set.
2426
///

0 commit comments

Comments
 (0)