Skip to content

Commit d45c1a1

Browse files
committed
[IntrinsicEmitter] Make AttributesMap bits adaptive
Make IntrinsicsToAttributesMap's func. and arg. fields be able to have adaptive sizes based on input other than hardcodded 8bits/8bits. This will ease the pressure for adding new intrinsics in private downstreams.
1 parent 8da3ab1 commit d45c1a1

File tree

3 files changed

+29
-24
lines changed

3 files changed

+29
-24
lines changed

llvm/lib/IR/Intrinsics.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -740,14 +740,6 @@ Intrinsic::ID Intrinsic::lookupIntrinsicID(StringRef Name) {
740740
#include "llvm/IR/IntrinsicImpl.inc"
741741
#undef GET_INTRINSIC_ATTRIBUTES
742742

743-
AttributeSet Intrinsic::getFnAttributes(LLVMContext &C, ID id) {
744-
if (id == 0)
745-
return AttributeSet();
746-
uint16_t PackedID = IntrinsicsToAttributesMap[id - 1];
747-
uint8_t FnAttrID = PackedID >> 8;
748-
return getIntrinsicFnAttributeSet(C, FnAttrID);
749-
}
750-
751743
Function *Intrinsic::getOrInsertDeclaration(Module *M, ID id,
752744
ArrayRef<Type *> Tys) {
753745
// There can never be multiple globals with the same name of different types,

llvm/test/TableGen/intrinsic-attrs.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ def int_deref_ptr_ret : Intrinsic<[llvm_ptr_ty], [], [Dereferenceable<RetIndex,
2525
// CHECK-NEXT: });
2626

2727
// CHECK: static constexpr uint16_t IntrinsicsToAttributesMap[] = {
28-
// CHECK: 0 << 8 | 0, // llvm.deref.ptr.ret
29-
// CHECK: 1 << 8 | 1, // llvm.random.gen
28+
// CHECK: 0 << 2 | 0, // llvm.deref.ptr.ret
29+
// CHECK: 1 << 2 | 1, // llvm.random.gen
3030
// CHECK: }; // IntrinsicsToAttributesMap
3131

3232
// CHECK: static constexpr ArgNoAttrIDPair ArgAttrIdTable[] = {

llvm/utils/TableGen/Basic/IntrinsicEmitter.cpp

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -629,21 +629,24 @@ static constexpr uint16_t IntrinsicsToAttributesMap[] = {)";
629629
UniqAttributes.try_emplace(&Int, ID);
630630
}
631631

632-
constexpr uint16_t NoFunctionAttrsID = 255;
633-
if (UniqAttributes.size() > 256)
634-
PrintFatalError("Too many unique argument attributes for table!");
635-
// Note, ID 255 is used to indicate no function attributes.
636-
if (UniqFnAttributes.size() > 255)
637-
PrintFatalError("Too many unique function attributes for table!");
638-
639-
// Assign a 16-bit packed ID for each intrinsic. The lower 8-bits will be its
640-
// "argument attribute ID" (index in UniqAttributes) and upper 8 bits will be
632+
const uint8_t UniqAttributesBitSize = Log2_32_Ceil(UniqAttributes.size() + 1);
633+
// Note, ID `-1` is used to indicate no function attributes.
634+
const uint8_t UniqFnAttributesBitSize =
635+
Log2_32_Ceil(UniqFnAttributes.size() + 2);
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
643+
// "argument attribute ID" (index in UniqAttributes) and upper bits will be
641644
// its "function attribute ID" (index in UniqFnAttributes).
642645
for (const CodeGenIntrinsic &Int : Ints) {
643646
uint16_t FnAttrIndex =
644647
hasFnAttributes(Int) ? UniqFnAttributes[&Int] : NoFunctionAttrsID;
645-
OS << formatv("\n {} << 8 | {}, // {}", FnAttrIndex,
646-
UniqAttributes[&Int], Int.Name);
648+
OS << formatv("\n {} << {} | {}, // {}", FnAttrIndex,
649+
UniqAttributesBitSize, UniqAttributes[&Int], Int.Name);
647650
}
648651

649652
OS << R"(
@@ -749,8 +752,8 @@ AttributeList Intrinsic::getAttributes(LLVMContext &C, ID id,
749752
return AttributeList();
750753
751754
uint16_t PackedID = IntrinsicsToAttributesMap[id - 1];
752-
uint8_t FnAttrID = PackedID >> 8;
753-
uint8_t ArgAttrID = PackedID & 0xFF;
755+
uint16_t FnAttrID = PackedID >> ({});
756+
uint16_t ArgAttrID = PackedID & ({});
754757
using PairTy = std::pair<unsigned, AttributeSet>;
755758
alignas(PairTy) char ASStorage[sizeof(PairTy) * {}];
756759
PairTy *AS = reinterpret_cast<PairTy *>(ASStorage);
@@ -772,10 +775,20 @@ AttributeList Intrinsic::getAttributes(LLVMContext &C, ID id,
772775
}
773776
return AttributeList::get(C, ArrayRef(AS, NumAttrs));
774777
}
778+
779+
AttributeSet Intrinsic::getFnAttributes(LLVMContext &C, ID id) {
780+
if (id == 0)
781+
return AttributeSet();
782+
uint16_t PackedID = IntrinsicsToAttributesMap[id - 1];
783+
uint16_t FnAttrID = PackedID >> ({});
784+
return getIntrinsicFnAttributeSet(C, FnAttrID);
785+
}
775786
#endif // GET_INTRINSIC_ATTRIBUTES
776787
777788
)",
778-
MaxNumAttrs, NoFunctionAttrsID);
789+
UniqAttributesBitSize,
790+
maskTrailingOnes<uint16_t>(UniqAttributesBitSize), MaxNumAttrs,
791+
NoFunctionAttrsID, UniqAttributesBitSize);
779792
}
780793

781794
void IntrinsicEmitter::EmitIntrinsicToBuiltinMap(

0 commit comments

Comments
 (0)