Skip to content

Commit e1b0fd9

Browse files
linehillsys-ce-bb
authored andcommitted
Use triple subarch to emit exact SPIR-V version (#2292)
Use target triple's subarch component, if present, for setting exact SPIR-V version for the SPIR-V emission. Resolves #1509. Original commit: KhronosGroup/SPIRV-LLVM-Translator@9f29f97
1 parent 0264197 commit e1b0fd9

File tree

5 files changed

+25
-5
lines changed

5 files changed

+25
-5
lines changed

llvm-spirv/lib/SPIRV/SPIRVWriter.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6471,6 +6471,7 @@ bool runSpirvWriterPasses(Module *M, std::ostream *OS, std::string &ErrMsg,
64716471
SPIRVEC_TripleMaxVersionIncompatible))
64726472
return false;
64736473
BM->setMinSPIRVVersion(ModuleVer);
6474+
BM->setMaxSPIRVVersion(ModuleVer);
64746475
}
64756476

64766477
ModulePassManager PassMgr;

llvm-spirv/lib/SPIRV/libSPIRV/SPIRVModule.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ class SPIRVModuleImpl : public SPIRVModule {
7979

8080
SPIRVModuleImpl(const SPIRV::TranslatorOpts &Opts) : SPIRVModuleImpl() {
8181
TranslationOpts = Opts;
82+
MaxVersion = Opts.getMaxVersion();
8283
}
8384

8485
~SPIRVModuleImpl() override;
@@ -2156,7 +2157,7 @@ std::istream &operator>>(std::istream &I, SPIRVModule &M) {
21562157
if (!M.getErrorLog().checkError(
21572158
SPIRVVersionIsAllowed, SPIRVEC_InvalidModule,
21582159
"incorrect SPIR-V version number " + to_string(MI.SPIRVVersion) +
2159-
" - it conflicts with --spirv-max-version which is set to " +
2160+
" - it conflicts with maximum allowed version which is set to " +
21602161
to_string(M.getMaximumAllowedSPIRVVersion()))) {
21612162
M.setInvalid();
21622163
return I;

llvm-spirv/lib/SPIRV/libSPIRV/SPIRVModule.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,12 @@ class SPIRVModule {
183183
setSPIRVVersion(std::max(static_cast<SPIRVWord>(Ver), getSPIRVVersion()));
184184
}
185185

186+
void setMaxSPIRVVersion(VersionNumber Ver) {
187+
assert(static_cast<SPIRVWord>(Ver) >= getSPIRVVersion() &&
188+
"Maximum version can't be lower than minimum version!");
189+
MaxVersion = std::min(Ver, MaxVersion);
190+
}
191+
186192
// Object creation functions
187193
template <class T> T *add(T *Entry) {
188194
addEntry(Entry);
@@ -487,16 +493,16 @@ class SPIRVModule {
487493

488494
virtual bool
489495
isAllowedToUseVersion(SPIRV::VersionNumber RequestedVersion) const final {
490-
return TranslationOpts.isAllowedToUseVersion(RequestedVersion);
496+
return RequestedVersion <= MaxVersion;
491497
}
492498

493499
virtual bool isAllowedToUseVersion(SPIRVWord RequestedVersion) const final {
494-
return TranslationOpts.isAllowedToUseVersion(
500+
return isAllowedToUseVersion(
495501
static_cast<SPIRV::VersionNumber>(RequestedVersion));
496502
}
497503

498504
virtual SPIRV::VersionNumber getMaximumAllowedSPIRVVersion() const final {
499-
return TranslationOpts.getMaxVersion();
505+
return MaxVersion;
500506
}
501507

502508
virtual bool
@@ -575,6 +581,7 @@ class SPIRVModule {
575581
bool ValidateCapability;
576582
bool AutoAddExtensions = true;
577583
SPIRV::TranslatorOpts TranslationOpts;
584+
VersionNumber MaxVersion = VersionNumber::MaximumVersion;
578585

579586
private:
580587
bool IsValid;

llvm-spirv/test/spirv-triple-with-version.ll

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,20 @@
1111
target datalayout = "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024"
1212
target triple = "spirv64v1.2-unknown-unknown"
1313

14+
; Check that the emitted SPIR-V version is not higher than the
15+
; requested due to a feature available in higher versions.
16+
define spir_func i32 @square(i32 %a) local_unnamed_addr #0 {
17+
entry:
18+
%mul = mul nuw nsw i32 %a, %a ; Optional integer wrap decorations require v1.4.
19+
ret i32 %mul
20+
}
21+
22+
attributes #0 = { norecurse nounwind readnone}
23+
1424
; This is a module with a SPIR-V subarch. Ensure that the SPIR-V version specified as the subarch is taken into account by llvm-spirv.
1525

1626
; CHECK-INVALID: TripleMaxVersionIncompatible: Triple version and maximum version are incompatible.
1727

1828
; 66048 = 0x10200, i.e. version 1.2
1929
; CHECK12: 119734787 66048
30+

llvm-spirv/test/spirv-version-controls.spt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@
3232
; RUN: llvm-spirv -r %t.spv --spirv-max-version=1.1 -o %t
3333
; RUN: not llvm-spirv -r %t.spv --spirv-max-version=1.0 -o - 2>&1 | FileCheck %s --check-prefix=CHECK-ERROR
3434
;
35-
; CHECK-ERROR: Invalid SPIR-V module: incorrect SPIR-V version number 1.1 (65792) - it conflicts with --spirv-max-version which is set to 1.0 (65536)
35+
; CHECK-ERROR: Invalid SPIR-V module: incorrect SPIR-V version number 1.1 (65792) - it conflicts with maximum allowed version which is set to 1.0 (65536)

0 commit comments

Comments
 (0)