Skip to content

Commit 0170bd5

Browse files
authored
[MDBuilder] mergeCallbackEncodings fails due to inspecting the wrong node (#92466)
Given the following metadata as, with `!6` as `ExistingCallbacks` and `!8` as `NewCB`: ``` !6 = !{!7} !7 = !{i64 0, i1 false} !8 = !{i64 2, i64 3, i1 false} ``` The merge function should add `!8` to the list of `!6`, i.e. `!6 = !{!7,!8}`. However, at the moment the check if this is legal, tries to interpret `!7` as integer instead of it's operand.
1 parent 79a3260 commit 0170bd5

File tree

2 files changed

+46
-7
lines changed

2 files changed

+46
-7
lines changed

llvm/lib/IR/MDBuilder.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,8 @@ MDNode *MDBuilder::createFunctionEntryCount(
8686
}
8787

8888
MDNode *MDBuilder::createFunctionSectionPrefix(StringRef Prefix) {
89-
return MDNode::get(Context,
90-
{createString("function_section_prefix"),
91-
createString(Prefix)});
89+
return MDNode::get(
90+
Context, {createString("function_section_prefix"), createString(Prefix)});
9291
}
9392

9493
MDNode *MDBuilder::createRange(const APInt &Lo, const APInt &Hi) {
@@ -148,9 +147,10 @@ MDNode *MDBuilder::mergeCallbackEncodings(MDNode *ExistingCallbacks,
148147
for (unsigned u = 0; u < NumExistingOps; u++) {
149148
Ops[u] = ExistingCallbacks->getOperand(u);
150149

151-
auto *OldCBCalleeIdxAsCM = cast<ConstantAsMetadata>(Ops[u]);
150+
auto *OldCBCalleeIdxAsCM =
151+
cast<ConstantAsMetadata>(cast<MDNode>(Ops[u])->getOperand(0));
152152
uint64_t OldCBCalleeIdx =
153-
cast<ConstantInt>(OldCBCalleeIdxAsCM->getValue())->getZExtValue();
153+
cast<ConstantInt>(OldCBCalleeIdxAsCM->getValue())->getZExtValue();
154154
(void)OldCBCalleeIdx;
155155
assert(NewCBCalleeIdx != OldCBCalleeIdx &&
156156
"Cannot map a callback callee index twice!");
@@ -339,8 +339,8 @@ MDNode *MDBuilder::createMutableTBAAAccessTag(MDNode *Tag) {
339339

340340
MDNode *MDBuilder::createIrrLoopHeaderWeight(uint64_t Weight) {
341341
Metadata *Vals[] = {
342-
createString("loop_header_weight"),
343-
createConstant(ConstantInt::get(Type::getInt64Ty(Context), Weight)),
342+
createString("loop_header_weight"),
343+
createConstant(ConstantInt::get(Type::getInt64Ty(Context), Weight)),
344344
};
345345
return MDNode::get(Context, Vals);
346346
}

llvm/unittests/IR/MDBuilderTest.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,43 @@ TEST_F(MDBuilderTest, createPCSections) {
127127
EXPECT_EQ(mdconst::extract<ConstantInt>(Aux->getOperand(1))->getValue(),
128128
C2->getValue());
129129
}
130+
TEST_F(MDBuilderTest, createCallbackAndMerge) {
131+
MDBuilder MDHelper(Context);
132+
auto *CB1 = MDHelper.createCallbackEncoding(0, {1, -1}, false);
133+
auto *CB2 = MDHelper.createCallbackEncoding(2, {-1}, false);
134+
ASSERT_EQ(CB1->getNumOperands(), 4U);
135+
ASSERT_TRUE(isa<ConstantAsMetadata>(CB1->getOperand(0)));
136+
ASSERT_TRUE(isa<ConstantAsMetadata>(CB1->getOperand(1)));
137+
ASSERT_TRUE(isa<ConstantAsMetadata>(CB1->getOperand(2)));
138+
ASSERT_TRUE(isa<ConstantAsMetadata>(CB1->getOperand(3)));
139+
EXPECT_EQ(mdconst::extract<ConstantInt>(CB1->getOperand(0))->getValue(), 0);
140+
EXPECT_EQ(mdconst::extract<ConstantInt>(CB1->getOperand(1))->getValue(), 1);
141+
EXPECT_EQ(mdconst::extract<ConstantInt>(CB1->getOperand(2))->getValue(), -1);
142+
EXPECT_EQ(mdconst::extract<ConstantInt>(CB1->getOperand(3))->getValue(),
143+
false);
144+
ASSERT_EQ(CB2->getNumOperands(), 3U);
145+
ASSERT_TRUE(isa<ConstantAsMetadata>(CB2->getOperand(0)));
146+
ASSERT_TRUE(isa<ConstantAsMetadata>(CB2->getOperand(1)));
147+
ASSERT_TRUE(isa<ConstantAsMetadata>(CB2->getOperand(2)));
148+
EXPECT_EQ(mdconst::extract<ConstantInt>(CB2->getOperand(0))->getValue(), 2);
149+
EXPECT_EQ(mdconst::extract<ConstantInt>(CB2->getOperand(1))->getValue(), -1);
150+
EXPECT_EQ(mdconst::extract<ConstantInt>(CB2->getOperand(2))->getValue(),
151+
false);
152+
auto *CBList = MDNode::get(Context, {CB1, CB2});
153+
auto *CB3 = MDHelper.createCallbackEncoding(4, {5}, false);
154+
auto *NewCBList = MDHelper.mergeCallbackEncodings(CBList, CB3);
155+
ASSERT_EQ(NewCBList->getNumOperands(), 3U);
156+
EXPECT_TRUE(NewCBList->getOperand(0) == CB1);
157+
EXPECT_TRUE(NewCBList->getOperand(1) == CB2);
158+
EXPECT_TRUE(NewCBList->getOperand(2) == CB3);
159+
160+
ASSERT_EQ(CB3->getNumOperands(), 3U);
161+
ASSERT_TRUE(isa<ConstantAsMetadata>(CB3->getOperand(0)));
162+
ASSERT_TRUE(isa<ConstantAsMetadata>(CB3->getOperand(1)));
163+
ASSERT_TRUE(isa<ConstantAsMetadata>(CB3->getOperand(2)));
164+
EXPECT_EQ(mdconst::extract<ConstantInt>(CB3->getOperand(0))->getValue(), 4);
165+
EXPECT_EQ(mdconst::extract<ConstantInt>(CB3->getOperand(1))->getValue(), 5);
166+
EXPECT_EQ(mdconst::extract<ConstantInt>(CB3->getOperand(2))->getValue(),
167+
false);
168+
}
130169
} // namespace

0 commit comments

Comments
 (0)