[IR] Allow MDString in operand bundles (#110805)

This change implements support of metadata strings in operand bundle
values. It makes possible calls like:

    call void @some_func(i32 %x) [ "foo"(i32 42, metadata !"abc") ]

It requires some extension of the bitcode serialization. As SSA values
and metadata are stored in different tables, there must be a way to
distinguish them during deserialization. It is implemented by putting a
special marker before the metadata index. The marker cannot be treated
as a reference to any SSA value, so it unambiguously identifies
metadata. It allows extending the bitcode serialization without breaking
compatibility.

Metadata as operand bundle values are intended to be used in
floating-point function calls. They would represent the same information
as now is passed by the constrained intrinsic arguments.
This commit is contained in:
Serge Pavlov 2024-10-11 12:09:10 +07:00 committed by GitHub
parent bf81bd800f
commit 15de239406
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 82 additions and 7 deletions

View File

@ -2666,8 +2666,8 @@ are grouped into a single :ref:`attribute group <attrgrp>`.
Operand Bundles Operand Bundles
--------------- ---------------
Operand bundles are tagged sets of SSA values that can be associated Operand bundles are tagged sets of SSA values or metadata strings that can be
with certain LLVM instructions (currently only ``call`` s and associated with certain LLVM instructions (currently only ``call`` s and
``invoke`` s). In a way they are like metadata, but dropping them is ``invoke`` s). In a way they are like metadata, but dropping them is
incorrect and will change program semantics. incorrect and will change program semantics.
@ -2675,7 +2675,7 @@ Syntax::
operand bundle set ::= '[' operand bundle (, operand bundle )* ']' operand bundle set ::= '[' operand bundle (, operand bundle )* ']'
operand bundle ::= tag '(' [ bundle operand ] (, bundle operand )* ')' operand bundle ::= tag '(' [ bundle operand ] (, bundle operand )* ')'
bundle operand ::= SSA value bundle operand ::= SSA value | metadata string
tag ::= string constant tag ::= string constant
Operand bundles are **not** part of a function's signature, and a Operand bundles are **not** part of a function's signature, and a

View File

@ -88,6 +88,8 @@ Changes to the LLVM IR
* `llvm.nvvm.ptr.shared.to.gen` * `llvm.nvvm.ptr.shared.to.gen`
* `llvm.nvvm.ptr.constant.to.gen` * `llvm.nvvm.ptr.constant.to.gen`
* `llvm.nvvm.ptr.local.to.gen` * `llvm.nvvm.ptr.local.to.gen`
* Operand bundle values can now be metadata strings.
Changes to LLVM infrastructure Changes to LLVM infrastructure
------------------------------ ------------------------------

View File

@ -529,6 +529,9 @@ enum PossiblyExactOperatorOptionalFlags { PEO_EXACT = 0 };
/// PossiblyDisjointInst's SubclassOptionalData contents. /// PossiblyDisjointInst's SubclassOptionalData contents.
enum PossiblyDisjointInstOptionalFlags { PDI_DISJOINT = 0 }; enum PossiblyDisjointInstOptionalFlags { PDI_DISJOINT = 0 };
/// Mark to distinguish metadata from value in an operator bundle.
enum MetadataOperandBundleValueMarker { OB_METADATA = 0x80000000 };
/// GetElementPtrOptionalFlags - Flags for serializing /// GetElementPtrOptionalFlags - Flags for serializing
/// GEPOperator's SubclassOptionalData contents. /// GEPOperator's SubclassOptionalData contents.
enum GetElementPtrOptionalFlags { enum GetElementPtrOptionalFlags {

View File

@ -3202,8 +3202,14 @@ bool LLParser::parseOptionalOperandBundles(
Type *Ty = nullptr; Type *Ty = nullptr;
Value *Input = nullptr; Value *Input = nullptr;
if (parseType(Ty) || parseValue(Ty, Input, PFS)) if (parseType(Ty))
return true; return true;
if (Ty->isMetadataTy()) {
if (parseMetadataAsValue(Input, PFS))
return true;
} else if (parseValue(Ty, Input, PFS)) {
return true;
}
Inputs.push_back(Input); Inputs.push_back(Input);
} }

View File

@ -792,6 +792,24 @@ private:
return ResVal == nullptr; return ResVal == nullptr;
} }
bool getValueOrMetadata(const SmallVectorImpl<uint64_t> &Record,
unsigned &Slot, unsigned InstNum, Value *&ResVal,
BasicBlock *ConstExprInsertBB) {
if (Slot == Record.size())
return true;
unsigned ValID = Record[Slot++];
if (ValID != bitc::OB_METADATA) {
unsigned TypeId;
return getValueTypePair(Record, --Slot, InstNum, ResVal, TypeId,
ConstExprInsertBB);
}
if (Slot == Record.size())
return true;
unsigned ValNo = InstNum - (unsigned)Record[Slot++];
ResVal = MetadataAsValue::get(Context, getFnMetadataByID(ValNo));
return false;
}
/// Read a value out of the specified record from slot 'Slot'. Increment Slot /// Read a value out of the specified record from slot 'Slot'. Increment Slot
/// past the number of slots used by the value in the record. Return true if /// past the number of slots used by the value in the record. Return true if
/// there is an error. /// there is an error.
@ -6767,8 +6785,7 @@ Error BitcodeReader::parseFunctionBody(Function *F) {
unsigned OpNum = 1; unsigned OpNum = 1;
while (OpNum != Record.size()) { while (OpNum != Record.size()) {
Value *Op; Value *Op;
unsigned OpTypeID; if (getValueOrMetadata(Record, OpNum, NextValueNo, Op, CurBB))
if (getValueTypePair(Record, OpNum, NextValueNo, Op, OpTypeID, CurBB))
return error("Invalid record"); return error("Invalid record");
Inputs.push_back(Op); Inputs.push_back(Op);
} }

View File

@ -395,6 +395,8 @@ private:
void writeModuleConstants(); void writeModuleConstants();
bool pushValueAndType(const Value *V, unsigned InstID, bool pushValueAndType(const Value *V, unsigned InstID,
SmallVectorImpl<unsigned> &Vals); SmallVectorImpl<unsigned> &Vals);
bool pushValueOrMetadata(const Value *V, unsigned InstID,
SmallVectorImpl<unsigned> &Vals);
void writeOperandBundles(const CallBase &CB, unsigned InstID); void writeOperandBundles(const CallBase &CB, unsigned InstID);
void pushValue(const Value *V, unsigned InstID, void pushValue(const Value *V, unsigned InstID,
SmallVectorImpl<unsigned> &Vals); SmallVectorImpl<unsigned> &Vals);
@ -2931,6 +2933,19 @@ bool ModuleBitcodeWriter::pushValueAndType(const Value *V, unsigned InstID,
return false; return false;
} }
bool ModuleBitcodeWriter::pushValueOrMetadata(const Value *V, unsigned InstID,
SmallVectorImpl<unsigned> &Vals) {
bool IsMetadata = V->getType()->isMetadataTy();
if (IsMetadata) {
Vals.push_back(bitc::OB_METADATA);
Metadata *MD = cast<MetadataAsValue>(V)->getMetadata();
unsigned ValID = VE.getMetadataID(MD);
Vals.push_back(InstID - ValID);
return false;
}
return pushValueAndType(V, InstID, Vals);
}
void ModuleBitcodeWriter::writeOperandBundles(const CallBase &CS, void ModuleBitcodeWriter::writeOperandBundles(const CallBase &CS,
unsigned InstID) { unsigned InstID) {
SmallVector<unsigned, 64> Record; SmallVector<unsigned, 64> Record;
@ -2941,7 +2956,7 @@ void ModuleBitcodeWriter::writeOperandBundles(const CallBase &CS,
Record.push_back(C.getOperandBundleTagID(Bundle.getTagName())); Record.push_back(C.getOperandBundleTagID(Bundle.getTagName()));
for (auto &Input : Bundle.Inputs) for (auto &Input : Bundle.Inputs)
pushValueAndType(Input, InstID, Record); pushValueOrMetadata(Input, InstID, Record);
Stream.EmitRecord(bitc::FUNC_CODE_OPERAND_BUNDLE, Record); Stream.EmitRecord(bitc::FUNC_CODE_OPERAND_BUNDLE, Record);
Record.clear(); Record.clear();

View File

@ -1327,6 +1327,14 @@ continue:
ret i32 0 ret i32 0
} }
declare void @instructions.bundles.callee(i32)
define void @instructions.bundles.metadata(i32 %x) {
entry:
call void @instructions.bundles.callee(i32 %x) [ "foo"(i32 42, metadata !"abc"), "bar"(metadata !"abcde", metadata !"qwerty") ]
; CHECK: call void @instructions.bundles.callee(i32 %x) [ "foo"(i32 42, metadata !"abc"), "bar"(metadata !"abcde", metadata !"qwerty") ]
ret void
}
; Instructions -- Unary Operations ; Instructions -- Unary Operations
define void @instructions.unops(double %op1) { define void @instructions.unops(double %op1) {
fneg double %op1 fneg double %op1

View File

@ -56,6 +56,13 @@ define void @f4(i32* %ptr) {
ret void ret void
} }
define void @f5(i32 %x) {
entry:
call void @callee1(i32 10, i32 %x) [ "foo"(i32 42, metadata !"abc"), "bar"(metadata !"abcde", metadata !"qwerty") ]
; CHECK: call void @callee1(i32 10, i32 %x) [ "foo"(i32 42, metadata !"abc"), "bar"(metadata !"abcde", metadata !"qwerty") ]
ret void
}
; Invoke versions of the above tests: ; Invoke versions of the above tests:
@ -150,3 +157,20 @@ exception:
normal: normal:
ret void ret void
} }
define void @g5(ptr %ptr) personality i8 3 {
entry:
%l = load i32, ptr %ptr, align 4
%x = add i32 42, 1
invoke void @callee1(i32 10, i32 %x) [ "foo"(i32 42, metadata !"abc"), "bar"(metadata !"abcde", metadata !"qwerty") ]
to label %normal unwind label %exception
; CHECK: invoke void @callee1(i32 10, i32 %x) [ "foo"(i32 42, metadata !"abc"), "bar"(metadata !"abcde", metadata !"qwerty") ]
exception: ; preds = %entry
%cleanup = landingpad i8
cleanup
br label %normal
normal: ; preds = %exception, %entry
ret void
}