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"
@@ -183,6 +184,8 @@ static cl::opt<std::string> SpecConst(
183
184
" SPIR-V module.\n "
184
185
" The list of valid ids is available via -spec-const-info option.\n "
185
186
" For duplicate ids the later one takes precedence.\n "
187
+ " Float values may be represented in decimal or hexadecimal, hex "
188
+ " values must be preceded by 0x.\n "
186
189
" Supported types are: i1, i8, i16, i32, i64, f16, f32, f64.\n " ),
187
190
cl::value_desc(" id1:type1:value1 id2:type2:value2 ..." ));
188
191
@@ -552,9 +555,9 @@ bool parseSpecConstOpt(llvm::StringRef SpecConstStr,
552
555
<< " \" must be a 32-bit unsigned integer\n " ;
553
556
return true ;
554
557
}
555
- auto It = std::find_if (
556
- SpecConstInfo.begin (), SpecConstInfo.end (),
557
- [=](SpecConstInfoTy Info) { return Info.first == SpecId; });
558
+ auto It =
559
+ std::find_if ( SpecConstInfo.begin (), SpecConstInfo.end (),
560
+ [=](SpecConstInfoTy Info) { return Info.ID == SpecId; });
558
561
if (It == SpecConstInfo.end ()) {
559
562
errs () << " Error: CL_INVALID_SPEC_ID. \" " << Option << " \" : There is no "
560
563
<< " specialization constant with id = " << SpecId
@@ -572,11 +575,11 @@ bool parseSpecConstOpt(llvm::StringRef SpecConstStr,
572
575
return true ;
573
576
}
574
577
size_t Size = Width < 8 ? 1 : Width / 8 ;
575
- if (Size != It->second ) {
578
+ if (Size != It->Size ) {
576
579
errs () << " Error: CL_INVALID_VALUE. In \" " << Option << " \" : Size of "
577
580
<< " type i" << Width << " (" << Size << " bytes) "
578
581
<< " does not match the size of the specialization constant "
579
- << " in the module (" << It->second << " bytes)\n " ;
582
+ << " in the module (" << It->Size << " bytes)\n " ;
580
583
return true ;
581
584
}
582
585
APInt Value;
@@ -611,21 +614,29 @@ bool parseSpecConstOpt(llvm::StringRef SpecConstStr,
611
614
return true ;
612
615
}
613
616
APFloat Value (*FS);
614
- Expected<APFloat::opStatus> StatusOrErr =
615
- Value.convertFromString (Params[2 ], APFloat::rmNearestTiesToEven);
616
- if (!StatusOrErr) {
617
- return true ;
618
- }
619
- // It's ok to have inexact conversion from decimal representation.
620
- APFloat::opStatus Status = *StatusOrErr;
621
- if (Status & ~APFloat::opInexact) {
622
- errs () << " Error: Invalid value for '-" << SpecConst.ArgStr
623
- << " ' option! In \" " << Option << " \" : can't convert \" "
624
- << Params[2 ] << " \" to " << Width
625
- << " -bit floating point number\n " ;
626
- return true ;
617
+ if (Params[2 ].find (" 0x" ) != StringRef::npos) {
618
+ std::stringstream paramStream;
619
+ paramStream << std::hex << Params[2 ].data ();
620
+ uint64_t specVal = 0 ;
621
+ paramStream >> specVal;
622
+ Opts.setSpecConst (SpecId, specVal);
623
+ } else {
624
+ Expected<APFloat::opStatus> StatusOrErr =
625
+ Value.convertFromString (Params[2 ], APFloat::rmNearestTiesToEven);
626
+ if (!StatusOrErr) {
627
+ return true ;
628
+ }
629
+ // It's ok to have inexact conversion from decimal representation.
630
+ APFloat::opStatus Status = *StatusOrErr;
631
+ if (Status & ~APFloat::opInexact) {
632
+ errs () << " Error: Invalid value for '-" << SpecConst.ArgStr
633
+ << " ' option! In \" " << Option << " \" : can't convert \" "
634
+ << Params[2 ] << " \" to " << Width
635
+ << " -bit floating point number\n " ;
636
+ return true ;
637
+ }
638
+ Opts.setSpecConst (SpecId, Value.bitcastToAPInt ().getZExtValue ());
627
639
}
628
- Opts.setSpecConst (SpecId, Value.bitcastToAPInt ().getZExtValue ());
629
640
} else {
630
641
errs () << " Error: Invalid type for '-" << SpecConst.ArgStr
631
642
<< " ' option! In \" " << Option << " \" : \" " << Params[1 ]
@@ -759,8 +770,9 @@ int main(int Ac, char **Av) {
759
770
std::cout << " Number of scalar specialization constants in the module = "
760
771
<< SpecConstInfo.size () << " \n " ;
761
772
for (auto &SpecConst : SpecConstInfo)
762
- std::cout << " Spec const id = " << SpecConst.first
763
- << " , size in bytes = " << SpecConst.second << " \n " ;
773
+ std::cout << " Spec const id = " << SpecConst.ID
774
+ << " , size in bytes = " << SpecConst.Size
775
+ << " , type = " << SpecConst.Type << " \n " ;
764
776
}
765
777
return 0 ;
766
778
}
0 commit comments