7575#include < map>
7676#include < memory>
7777#include < set>
78+ #include < sstream>
7879#include < string>
7980
8081#define DEBUG_TYPE " spirv"
@@ -190,6 +191,8 @@ static cl::opt<std::string> SpecConst(
190191 " SPIR-V module.\n "
191192 " The list of valid ids is available via -spec-const-info option.\n "
192193 " 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 "
193196 " Supported types are: i1, i8, i16, i32, i64, f16, f32, f64.\n " ),
194197 cl::value_desc(" id1:type1:value1 id2:type2:value2 ..." ));
195198
@@ -559,9 +562,9 @@ bool parseSpecConstOpt(llvm::StringRef SpecConstStr,
559562 << " \" must be a 32-bit unsigned integer\n " ;
560563 return true ;
561564 }
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; });
565568 if (It == SpecConstInfo.end ()) {
566569 errs () << " Error: CL_INVALID_SPEC_ID. \" " << Option << " \" : There is no "
567570 << " specialization constant with id = " << SpecId
@@ -579,11 +582,11 @@ bool parseSpecConstOpt(llvm::StringRef SpecConstStr,
579582 return true ;
580583 }
581584 size_t Size = Width < 8 ? 1 : Width / 8 ;
582- if (Size != It->second ) {
585+ if (Size != It->Size ) {
583586 errs () << " Error: CL_INVALID_VALUE. In \" " << Option << " \" : Size of "
584587 << " type i" << Width << " (" << Size << " bytes) "
585588 << " 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 " ;
587590 return true ;
588591 }
589592 APInt Value;
@@ -618,21 +621,29 @@ bool parseSpecConstOpt(llvm::StringRef SpecConstStr,
618621 return true ;
619622 }
620623 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 ());
634646 }
635- Opts.setSpecConst (SpecId, Value.bitcastToAPInt ().getZExtValue ());
636647 } else {
637648 errs () << " Error: Invalid type for '-" << SpecConst.ArgStr
638649 << " ' option! In \" " << Option << " \" : \" " << Params[1 ]
@@ -766,8 +777,9 @@ int main(int Ac, char **Av) {
766777 std::cout << " Number of scalar specialization constants in the module = "
767778 << SpecConstInfo.size () << " \n " ;
768779 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 " ;
771783 }
772784 return 0 ;
773785}
0 commit comments