Skip to content

Commit 9f04195

Browse files
committed
[IntrinsicEmitter] Make AttributesMap PackedID type-adaptive
Following up #157965 which made this bit-adaptive, this patch attempts to make this map able to choose type ranging from int8 to int64 based on intrinsic inputs.
1 parent 3fe85ca commit 9f04195

File tree

2 files changed

+33
-22
lines changed

2 files changed

+33
-22
lines changed

llvm/test/TableGen/intrinsic-attrs.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def int_deref_ptr_ret : Intrinsic<[llvm_ptr_ty], [], [Dereferenceable<RetIndex,
2424
// CHECK-NEXT: Attribute::get(C, Attribute::NoUnwind),
2525
// CHECK-NEXT: });
2626

27-
// CHECK: static constexpr uint16_t IntrinsicsToAttributesMap[] = {
27+
// CHECK: static constexpr uint8_t IntrinsicsToAttributesMap[] = {
2828
// CHECK: 0 << 1 | 0, // llvm.deref.ptr.ret
2929
// CHECK: 1 << 1 | 1, // llvm.random.gen
3030
// CHECK: }; // IntrinsicsToAttributesMap

llvm/utils/TableGen/Basic/IntrinsicEmitter.cpp

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -617,9 +617,7 @@ static AttributeSet getIntrinsicFnAttributeSet(LLVMContext &C, unsigned ID) {
617617
}
618618
OS << R"(
619619
}
620-
} // getIntrinsicFnAttributeSet
621-
622-
static constexpr uint16_t IntrinsicsToAttributesMap[] = {)";
620+
} // getIntrinsicFnAttributeSet)";
623621

624622
// Compute unique argument attributes.
625623
std::map<const CodeGenIntrinsic *, unsigned, AttributeComparator>
@@ -630,20 +628,29 @@ static constexpr uint16_t IntrinsicsToAttributesMap[] = {)";
630628
}
631629

632630
const uint8_t UniqAttributesBitSize = Log2_32_Ceil(UniqAttributes.size());
633-
// Note, ID `-1` is used to indicate no function attributes.
631+
// Note, max value is used to indicate no function attributes.
634632
const uint8_t UniqFnAttributesBitSize =
635633
Log2_32_Ceil(UniqFnAttributes.size() + 1);
636-
const uint16_t NoFunctionAttrsID =
637-
maskTrailingOnes<uint16_t>(UniqFnAttributesBitSize);
638-
if (UniqAttributesBitSize + UniqFnAttributesBitSize > 16)
639-
PrintFatalError(
640-
"More than 16 bits are used for IntrinsicsToAttributesMap's entry!");
641-
642-
// Assign a 16-bit packed ID for each intrinsic. The lower bits will be its
634+
const uint32_t NoFunctionAttrsID =
635+
maskTrailingOnes<uint32_t>(UniqFnAttributesBitSize);
636+
uint8_t AttributesMapDataBitSize =
637+
PowerOf2Ceil(UniqAttributesBitSize + UniqFnAttributesBitSize);
638+
if (AttributesMapDataBitSize < 8)
639+
AttributesMapDataBitSize = 8;
640+
else if (AttributesMapDataBitSize > 64)
641+
PrintFatalError("Packed ID of IntrinsicsToAttributesMap exceeds 64b!");
642+
else if (AttributesMapDataBitSize > 16)
643+
PrintWarning("Packed ID of IntrinsicsToAttributesMap exceeds 16b, "
644+
"this may cause performance drop (pr106809), "
645+
"please consider redesigning intrinsic sets!");
646+
647+
// Assign a packed ID for each intrinsic. The lower bits will be its
643648
// "argument attribute ID" (index in UniqAttributes) and upper bits will be
644649
// its "function attribute ID" (index in UniqFnAttributes).
650+
OS << formatv("\nstatic constexpr uint{}_t IntrinsicsToAttributesMap[] = {{",
651+
AttributesMapDataBitSize);
645652
for (const CodeGenIntrinsic &Int : Ints) {
646-
uint16_t FnAttrIndex =
653+
uint32_t FnAttrIndex =
647654
hasFnAttributes(Int) ? UniqFnAttributes[&Int] : NoFunctionAttrsID;
648655
OS << formatv("\n {} << {} | {}, // {}", FnAttrIndex,
649656
UniqAttributesBitSize, UniqAttributes[&Int], Int.Name);
@@ -746,14 +753,19 @@ static constexpr ArgAttributesInfo ArgAttributesInfoTable[] = {{
746753
// construct all the argument attributes (using the ArgAttributesInfoTable and
747754
// ArgAttrIdTable) and then add on the function attributes if any.
748755
OS << formatv(R"(
756+
757+
template <typename IDTy>
758+
inline std::pair<uint32_t, uint32_t> unpackID(IDTy PackedID) {{
759+
uint32_t FnAttrID = PackedID >> ({});
760+
uint32_t ArgAttrID = PackedID & ({});
761+
return {{FnAttrID, ArgAttrID};
762+
}
763+
749764
AttributeList Intrinsic::getAttributes(LLVMContext &C, ID id,
750765
FunctionType *FT) {{
751766
if (id == 0)
752767
return AttributeList();
753-
754-
uint16_t PackedID = IntrinsicsToAttributesMap[id - 1];
755-
uint16_t FnAttrID = PackedID >> ({});
756-
uint16_t ArgAttrID = PackedID & ({});
768+
auto [FnAttrID, ArgAttrID] = unpackID(IntrinsicsToAttributesMap[id - 1]);
757769
using PairTy = std::pair<unsigned, AttributeSet>;
758770
alignas(PairTy) char ASStorage[sizeof(PairTy) * {}];
759771
PairTy *AS = reinterpret_cast<PairTy *>(ASStorage);
@@ -776,19 +788,18 @@ AttributeList Intrinsic::getAttributes(LLVMContext &C, ID id,
776788
return AttributeList::get(C, ArrayRef(AS, NumAttrs));
777789
}
778790
779-
AttributeSet Intrinsic::getFnAttributes(LLVMContext &C, ID id) {
791+
AttributeSet Intrinsic::getFnAttributes(LLVMContext &C, ID id) {{
780792
if (id == 0)
781793
return AttributeSet();
782-
uint16_t PackedID = IntrinsicsToAttributesMap[id - 1];
783-
uint16_t FnAttrID = PackedID >> ({});
794+
auto [FnAttrID, ArgAttrID] = unpackID(IntrinsicsToAttributesMap[id - 1]);
784795
return getIntrinsicFnAttributeSet(C, FnAttrID);
785796
}
786797
#endif // GET_INTRINSIC_ATTRIBUTES
787798
788799
)",
789800
UniqAttributesBitSize,
790-
maskTrailingOnes<uint16_t>(UniqAttributesBitSize), MaxNumAttrs,
791-
NoFunctionAttrsID, UniqAttributesBitSize);
801+
maskTrailingOnes<uint32_t>(UniqAttributesBitSize),
802+
MaxNumAttrs, NoFunctionAttrsID);
792803
}
793804

794805
void IntrinsicEmitter::EmitIntrinsicToBuiltinMap(

0 commit comments

Comments
 (0)