75
75
#include < map>
76
76
#include < memory>
77
77
#include < set>
78
+ #include < sstream>
78
79
#include < string>
79
80
80
81
#define DEBUG_TYPE " spirv"
@@ -190,6 +191,8 @@ static cl::opt<std::string> SpecConst(
190
191
" SPIR-V module.\n "
191
192
" The list of valid ids is available via -spec-const-info option.\n "
192
193
" For duplicate ids the later one takes precedence.\n "
194
+ " Float values may be represented in decimal or hexadecimal, hex "
195
+ " values must be preceded by 0x.\n "
193
196
" Supported types are: i1, i8, i16, i32, i64, f16, f32, f64.\n " ),
194
197
cl::value_desc(" id1:type1:value1 id2:type2:value2 ..." ));
195
198
@@ -559,9 +562,9 @@ bool parseSpecConstOpt(llvm::StringRef SpecConstStr,
559
562
<< " \" must be a 32-bit unsigned integer\n " ;
560
563
return true ;
561
564
}
562
- auto It = std::find_if (
563
- SpecConstInfo.begin (), SpecConstInfo.end (),
564
- [=](SpecConstInfoTy Info) { return Info.first == SpecId; });
565
+ auto It =
566
+ std::find_if ( SpecConstInfo.begin (), SpecConstInfo.end (),
567
+ [=](SpecConstInfoTy Info) { return Info.ID == SpecId; });
565
568
if (It == SpecConstInfo.end ()) {
566
569
errs () << " Error: CL_INVALID_SPEC_ID. \" " << Option << " \" : There is no "
567
570
<< " specialization constant with id = " << SpecId
@@ -579,11 +582,11 @@ bool parseSpecConstOpt(llvm::StringRef SpecConstStr,
579
582
return true ;
580
583
}
581
584
size_t Size = Width < 8 ? 1 : Width / 8 ;
582
- if (Size != It->second ) {
585
+ if (Size != It->Size ) {
583
586
errs () << " Error: CL_INVALID_VALUE. In \" " << Option << " \" : Size of "
584
587
<< " type i" << Width << " (" << Size << " bytes) "
585
588
<< " does not match the size of the specialization constant "
586
- << " in the module (" << It->second << " bytes)\n " ;
589
+ << " in the module (" << It->Size << " bytes)\n " ;
587
590
return true ;
588
591
}
589
592
APInt Value;
@@ -618,21 +621,29 @@ bool parseSpecConstOpt(llvm::StringRef SpecConstStr,
618
621
return true ;
619
622
}
620
623
APFloat Value (*FS);
621
- Expected<APFloat::opStatus> StatusOrErr =
622
- Value.convertFromString (Params[2 ], APFloat::rmNearestTiesToEven);
623
- if (!StatusOrErr) {
624
- return true ;
625
- }
626
- // It's ok to have inexact conversion from decimal representation.
627
- APFloat::opStatus Status = *StatusOrErr;
628
- if (Status & ~APFloat::opInexact) {
629
- errs () << " Error: Invalid value for '-" << SpecConst.ArgStr
630
- << " ' option! In \" " << Option << " \" : can't convert \" "
631
- << Params[2 ] << " \" to " << Width
632
- << " -bit floating point number\n " ;
633
- return true ;
624
+ if (Params[2 ].find (" 0x" ) != StringRef::npos) {
625
+ std::stringstream paramStream;
626
+ paramStream << std::hex << Params[2 ].data ();
627
+ uint64_t specVal = 0 ;
628
+ paramStream >> specVal;
629
+ Opts.setSpecConst (SpecId, specVal);
630
+ } else {
631
+ Expected<APFloat::opStatus> StatusOrErr =
632
+ Value.convertFromString (Params[2 ], APFloat::rmNearestTiesToEven);
633
+ if (!StatusOrErr) {
634
+ return true ;
635
+ }
636
+ // It's ok to have inexact conversion from decimal representation.
637
+ APFloat::opStatus Status = *StatusOrErr;
638
+ if (Status & ~APFloat::opInexact) {
639
+ errs () << " Error: Invalid value for '-" << SpecConst.ArgStr
640
+ << " ' option! In \" " << Option << " \" : can't convert \" "
641
+ << Params[2 ] << " \" to " << Width
642
+ << " -bit floating point number\n " ;
643
+ return true ;
644
+ }
645
+ Opts.setSpecConst (SpecId, Value.bitcastToAPInt ().getZExtValue ());
634
646
}
635
- Opts.setSpecConst (SpecId, Value.bitcastToAPInt ().getZExtValue ());
636
647
} else {
637
648
errs () << " Error: Invalid type for '-" << SpecConst.ArgStr
638
649
<< " ' option! In \" " << Option << " \" : \" " << Params[1 ]
@@ -766,8 +777,9 @@ int main(int Ac, char **Av) {
766
777
std::cout << " Number of scalar specialization constants in the module = "
767
778
<< SpecConstInfo.size () << " \n " ;
768
779
for (auto &SpecConst : SpecConstInfo)
769
- std::cout << " Spec const id = " << SpecConst.first
770
- << " , size in bytes = " << SpecConst.second << " \n " ;
780
+ std::cout << " Spec const id = " << SpecConst.ID
781
+ << " , size in bytes = " << SpecConst.Size
782
+ << " , type = " << SpecConst.Type << " \n " ;
771
783
}
772
784
return 0 ;
773
785
}
0 commit comments