@@ -763,12 +763,47 @@ void VISAKernelImpl::createBindlessSampler()
763763 }
764764}
765765
766+ void VISAKernelImpl::createReservedKeywordSet () {
767+ for (int i = 0 ; i < ISA_NUM_OPCODE; i++) {
768+ const VISA_INST_Desc &desc = CISA_INST_table[i];
769+ if (desc.name != nullptr )
770+ reservedNames.insert (desc.name );
771+ int subOpsLen = 0 ;
772+ const ISA_SubInst_Desc *subOps = getSubInstTable (desc.opcode , subOpsLen);
773+ if (subOps != nullptr ) {
774+ // e.g. ops like ISA_SVM have a subtable of operations
775+ for (int si = 0 ; si < subOpsLen; si++) {
776+ // same tables have some padding and empty ops
777+ if (subOps[si].name != nullptr )
778+ reservedNames.insert (subOps[si].name );
779+ }
780+ }
781+ }
782+
783+ }
784+
785+ bool VISAKernelImpl::isReservedName (const std::string &nm) const {
786+ auto opItr = reservedNames.find (nm);
787+ return (opItr != reservedNames.end ());
788+ }
789+
766790void VISAKernelImpl::ensureVariableNameUnique (const char *&varName)
767791{
768- // escape the name to ensure it's a legal vISA identifier
792+ // legalize the LLVM name to vISA standards; some examples follow:
793+ // given ==> we fix it to this
794+ // 1. "0" ==> "_0" (LLVM name)
795+ // 2. "add.i.i" ==> "add_i_i" (LLVM compound name)
796+ // 3. "mul" ==> "mul_" (vISA keyword)
797+ // 4. suppose both variable "x" and "x0" exist
798+ // "x" ==> "x_1" (since "x0" already used)
799+ // "x0" ==> "x0_1" (it's a dumb suffixing strategy)
769800 std::stringstream escdName;
801+
802+ // step 1
770803 if (isdigit (varName[0 ]))
771804 escdName << ' _' ;
805+
806+ // step 2
772807 for (size_t i = 0 , slen = strlen (varName); i < slen; i++) {
773808 char c = varName[i];
774809 if (!isalnum (c)) {
@@ -777,6 +812,11 @@ void VISAKernelImpl::ensureVariableNameUnique(const char *&varName)
777812 escdName << c;
778813 }
779814
815+ // case 3: "mul" ==> "mul_"
816+ while (isReservedName (escdName.str ()))
817+ escdName << ' _' ;
818+
819+ // case 4: if "x" already exists, then use "x_#" where # is 0,1,..
780820 std::string varNameS = escdName.str ();
781821 if (varNames.find (varNameS) != varNames.end ()) {
782822 // not unqiue, add a counter until it is unique
@@ -787,14 +827,12 @@ void VISAKernelImpl::ensureVariableNameUnique(const char *&varName)
787827 varNameS = ss.str ();
788828 } while (varNames.find (varNameS) != varNames.end ());
789829 }
790-
791830 varNames.insert (varNameS);
792831
793832 char *buf = (char *)m_mem.alloc (varNameS.size () + 1 );
794833 memcpy_s (buf, varNameS.size (), varNameS.c_str (), varNameS.size ());
795834 buf[varNameS.size ()] = 0 ;
796835 varName = buf;
797- varNames.insert (varNameS);
798836}
799837
800838void VISAKernelImpl::generateVariableName (Common_ISA_Var_Class Ty, const char *&varName)
0 commit comments