Skip to content

Commit 1442b0e

Browse files
authored
[TableGen] Remove redundant buffer copies for ULEB128 decode calls. (#80199)
This patch removes a couple of redundant buffer copies in emitTable for setting up calls to decodeULEB128. Instead, provide the Table.data buffer directly to the calls-- where decodeULEB128 does its own buffer overflow checking. Factor out 7 explicit loops to emit ULEB128 bytes into emitULEB128. Also factor out 4 copies of 24-bit numtoskip emission into emitNumToSkip. The functionality is already covered by existing unit tests and by virtue of most of the in-tree back-ends exercising the decoder emitter.
1 parent 617602d commit 1442b0e

File tree

1 file changed

+52
-68
lines changed

1 file changed

+52
-68
lines changed

llvm/utils/TableGen/DecoderEmitter.cpp

Lines changed: 52 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,33 @@ void DecoderEmitter::emitTable(formatted_raw_ostream &OS, DecoderTable &Table,
775775

776776
Indentation += 2;
777777

778+
// Emit ULEB128 encoded value to OS, returning the number of bytes emitted.
779+
auto emitULEB128 = [](DecoderTable::const_iterator I,
780+
formatted_raw_ostream &OS) {
781+
unsigned Len = 0;
782+
while (*I >= 128) {
783+
OS << (unsigned)*I++ << ", ";
784+
Len++;
785+
}
786+
OS << (unsigned)*I++ << ", ";
787+
return Len + 1;
788+
};
789+
790+
// Emit 24-bit numtoskip value to OS, returning the NumToSkip value.
791+
auto emitNumToSkip = [](DecoderTable::const_iterator I,
792+
formatted_raw_ostream &OS) {
793+
uint8_t Byte = *I++;
794+
uint32_t NumToSkip = Byte;
795+
OS << (unsigned)Byte << ", ";
796+
Byte = *I++;
797+
OS << (unsigned)Byte << ", ";
798+
NumToSkip |= Byte << 8;
799+
Byte = *I++;
800+
OS << utostr(Byte) << ", ";
801+
NumToSkip |= Byte << 16;
802+
return NumToSkip;
803+
};
804+
778805
// FIXME: We may be able to use the NumToSkip values to recover
779806
// appropriate indentation levels.
780807
DecoderTable::const_iterator I = Table.begin();
@@ -794,14 +821,11 @@ void DecoderEmitter::emitTable(formatted_raw_ostream &OS, DecoderTable &Table,
794821
OS.indent(Indentation) << "MCD::OPC_ExtractField, ";
795822

796823
// ULEB128 encoded start value.
797-
uint8_t Buffer[16], *P = Buffer;
798-
while ((*P++ = *I++) >= 128)
799-
assert((P - Buffer) <= (ptrdiff_t)sizeof(Buffer) &&
800-
"ULEB128 value too large!");
801-
unsigned Start = decodeULEB128(Buffer);
802-
for (P = Buffer; *P >= 128; ++P)
803-
OS << (unsigned)*P << ", ";
804-
OS << (unsigned)*P << ", ";
824+
const char *ErrMsg = nullptr;
825+
unsigned Start = decodeULEB128(Table.data() + Pos + 1, nullptr,
826+
Table.data() + Table.size(), &ErrMsg);
827+
assert(ErrMsg == nullptr && "ULEB128 value too large!");
828+
I += emitULEB128(I, OS);
805829

806830
unsigned Len = *I++;
807831
OS << Len << ", // Inst{";
@@ -814,91 +838,58 @@ void DecoderEmitter::emitTable(formatted_raw_ostream &OS, DecoderTable &Table,
814838
++I;
815839
OS.indent(Indentation) << "MCD::OPC_FilterValue, ";
816840
// The filter value is ULEB128 encoded.
817-
while (*I >= 128)
818-
OS << (unsigned)*I++ << ", ";
819-
OS << (unsigned)*I++ << ", ";
841+
I += emitULEB128(I, OS);
820842

821843
// 24-bit numtoskip value.
822-
uint8_t Byte = *I++;
823-
uint32_t NumToSkip = Byte;
824-
OS << (unsigned)Byte << ", ";
825-
Byte = *I++;
826-
OS << (unsigned)Byte << ", ";
827-
NumToSkip |= Byte << 8;
828-
Byte = *I++;
829-
OS << utostr(Byte) << ", ";
830-
NumToSkip |= Byte << 16;
844+
uint32_t NumToSkip = emitNumToSkip(I, OS);
845+
I += 3;
831846
OS << "// Skip to: " << ((I - Table.begin()) + NumToSkip) << "\n";
832847
break;
833848
}
834849
case MCD::OPC_CheckField: {
835850
++I;
836851
OS.indent(Indentation) << "MCD::OPC_CheckField, ";
837852
// ULEB128 encoded start value.
838-
for (; *I >= 128; ++I)
839-
OS << (unsigned)*I << ", ";
840-
OS << (unsigned)*I++ << ", ";
853+
I += emitULEB128(I, OS);
841854
// 8-bit length.
842855
unsigned Len = *I++;
843856
OS << Len << ", ";
844857
// ULEB128 encoded field value.
845-
for (; *I >= 128; ++I)
846-
OS << (unsigned)*I << ", ";
847-
OS << (unsigned)*I++ << ", ";
858+
I += emitULEB128(I, OS);
859+
848860
// 24-bit numtoskip value.
849-
uint8_t Byte = *I++;
850-
uint32_t NumToSkip = Byte;
851-
OS << (unsigned)Byte << ", ";
852-
Byte = *I++;
853-
OS << (unsigned)Byte << ", ";
854-
NumToSkip |= Byte << 8;
855-
Byte = *I++;
856-
OS << utostr(Byte) << ", ";
857-
NumToSkip |= Byte << 16;
861+
uint32_t NumToSkip = emitNumToSkip(I, OS);
862+
I += 3;
858863
OS << "// Skip to: " << ((I - Table.begin()) + NumToSkip) << "\n";
859864
break;
860865
}
861866
case MCD::OPC_CheckPredicate: {
862867
++I;
863868
OS.indent(Indentation) << "MCD::OPC_CheckPredicate, ";
864-
for (; *I >= 128; ++I)
865-
OS << (unsigned)*I << ", ";
866-
OS << (unsigned)*I++ << ", ";
869+
I += emitULEB128(I, OS);
867870

868871
// 24-bit numtoskip value.
869-
uint8_t Byte = *I++;
870-
uint32_t NumToSkip = Byte;
871-
OS << (unsigned)Byte << ", ";
872-
Byte = *I++;
873-
OS << (unsigned)Byte << ", ";
874-
NumToSkip |= Byte << 8;
875-
Byte = *I++;
876-
OS << utostr(Byte) << ", ";
877-
NumToSkip |= Byte << 16;
872+
uint32_t NumToSkip = emitNumToSkip(I, OS);
873+
I += 3;
878874
OS << "// Skip to: " << ((I - Table.begin()) + NumToSkip) << "\n";
879875
break;
880876
}
881877
case MCD::OPC_Decode:
882878
case MCD::OPC_TryDecode: {
883879
bool IsTry = *I == MCD::OPC_TryDecode;
884880
++I;
885-
// Extract the ULEB128 encoded Opcode to a buffer.
886-
uint8_t Buffer[16], *p = Buffer;
887-
while ((*p++ = *I++) >= 128)
888-
assert((p - Buffer) <= (ptrdiff_t)sizeof(Buffer)
889-
&& "ULEB128 value too large!");
890881
// Decode the Opcode value.
891-
unsigned Opc = decodeULEB128(Buffer);
882+
const char *ErrMsg = nullptr;
883+
unsigned Opc = decodeULEB128(Table.data() + Pos + 1, nullptr,
884+
Table.data() + Table.size(), &ErrMsg);
885+
assert(ErrMsg == nullptr && "ULEB128 value too large!");
886+
892887
OS.indent(Indentation) << "MCD::OPC_" << (IsTry ? "Try" : "")
893888
<< "Decode, ";
894-
for (p = Buffer; *p >= 128; ++p)
895-
OS << (unsigned)*p << ", ";
896-
OS << (unsigned)*p << ", ";
889+
I += emitULEB128(I, OS);
897890

898891
// Decoder index.
899-
for (; *I >= 128; ++I)
900-
OS << (unsigned)*I << ", ";
901-
OS << (unsigned)*I++ << ", ";
892+
I += emitULEB128(I, OS);
902893

903894
if (!IsTry) {
904895
OS << "// Opcode: " << NumberedEncodings[Opc] << "\n";
@@ -908,15 +899,8 @@ void DecoderEmitter::emitTable(formatted_raw_ostream &OS, DecoderTable &Table,
908899
// Fallthrough for OPC_TryDecode.
909900

910901
// 24-bit numtoskip value.
911-
uint8_t Byte = *I++;
912-
uint32_t NumToSkip = Byte;
913-
OS << (unsigned)Byte << ", ";
914-
Byte = *I++;
915-
OS << (unsigned)Byte << ", ";
916-
NumToSkip |= Byte << 8;
917-
Byte = *I++;
918-
OS << utostr(Byte) << ", ";
919-
NumToSkip |= Byte << 16;
902+
uint32_t NumToSkip = emitNumToSkip(I, OS);
903+
I += 3;
920904

921905
OS << "// Opcode: " << NumberedEncodings[Opc]
922906
<< ", skip to: " << ((I - Table.begin()) + NumToSkip) << "\n";

0 commit comments

Comments
 (0)