Debian Codegraph @

llvm/unittests/Analysis/MemoryProfileInfoTest.cpp

raw
1 //===- MemoryProfileInfoTest.cpp - Memory Profile Info Unit Tests-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/Analysis/MemoryProfileInfo.h"
10 #include "llvm/AsmParser/Parser.h"
11 #include "llvm/IR/Constants.h"
12 #include "llvm/IR/Instructions.h"
13 #include "llvm/IR/LLVMContext.h"
14 #include "llvm/IR/Module.h"
15 #include "llvm/IR/ModuleSummaryIndex.h"
16 #include "llvm/Support/CommandLine.h"
17 #include "llvm/Support/Compiler.h"
18 #include "llvm/Support/SourceMgr.h"
19 #include "gmock/gmock.h"
20 #include "gtest/gtest.h"
21 #include <cstring>
22 #include <sys/types.h>
23
24 using namespace llvm;
25 using namespace llvm::memprof;
26
27 LLVM_ABI extern cl::opt<bool> MemProfKeepAllNotColdContexts;
28
29 namespace {
30
31 class MemoryProfileInfoTest : public testing::Test {
32 protected:
33 std::unique_ptr<Module> makeLLVMModule(LLVMContext &C, const char *IR) {
34 SMDiagnostic Err;
35 std::unique_ptr<Module> Mod = parseAssemblyString(IR, Err, C);
36 if (!Mod)
37 Err.print("MemoryProfileInfoTest", errs());
38 return Mod;
39 }
40
41 std::unique_ptr<ModuleSummaryIndex> makeLLVMIndex(const char *Summary) {
42 SMDiagnostic Err;
43 std::unique_ptr<ModuleSummaryIndex> Index =
44 parseSummaryIndexAssemblyString(Summary, Err);
45 if (!Index)
46 Err.print("MemoryProfileInfoTest", errs());
47 return Index;
48 }
49
50 // This looks for a call that has the given value name, which
51 // is the name of the value being assigned the call return value.
52 CallBase *findCall(Function &F, const char *Name = nullptr) {
53 for (auto &BB : F)
54 for (auto &I : BB)
55 if (auto *CB = dyn_cast<CallBase>(&I))
56 if (!Name || CB->getName() == Name)
57 return CB;
58 return nullptr;
59 }
60 };
61
62 // Test the hasSingleAllocType helper.
63 TEST_F(MemoryProfileInfoTest, SingleAllocType) {
64 uint8_t NotCold = (uint8_t)AllocationType::NotCold;
65 uint8_t Cold = (uint8_t)AllocationType::Cold;
66 uint8_t Hot = (uint8_t)AllocationType::Hot;
67 EXPECT_TRUE(hasSingleAllocType(NotCold));
68 EXPECT_TRUE(hasSingleAllocType(Cold));
69 EXPECT_TRUE(hasSingleAllocType(Hot));
70 EXPECT_FALSE(hasSingleAllocType(NotCold | Cold));
71 EXPECT_FALSE(hasSingleAllocType(NotCold | Hot));
72 EXPECT_FALSE(hasSingleAllocType(Cold | Hot));
73 EXPECT_FALSE(hasSingleAllocType(NotCold | Cold | Hot));
74 }
75
76 // Test buildCallstackMetadata helper.
77 TEST_F(MemoryProfileInfoTest, BuildCallStackMD) {
78 LLVMContext C;
79 MDNode *CallStack = buildCallstackMetadata({1, 2, 3}, C);
80 ASSERT_EQ(CallStack->getNumOperands(), 3u);
81 unsigned ExpectedId = 1;
82 for (auto &Op : CallStack->operands()) {
83 auto *StackId = mdconst::dyn_extract<ConstantInt>(Op);
84 EXPECT_EQ(StackId->getZExtValue(), ExpectedId++);
85 }
86 }
87
88 // Test CallStackTrie::addCallStack interface taking allocation type and list of
89 // call stack ids.
90 // Check that allocations with a single allocation type along all call stacks
91 // get an attribute instead of memprof metadata.
92 TEST_F(MemoryProfileInfoTest, Attribute) {
93 LLVMContext C;
94 std::unique_ptr<Module> M = makeLLVMModule(C,
95 R"IR(
96 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
97 target triple = "x86_64-pc-linux-gnu"
98 define i32* @test() {
99 entry:
100 %call1 = call noalias dereferenceable_or_null(40) i8* @malloc(i64 noundef 40)
101 %0 = bitcast i8* %call1 to i32*
102 %call2 = call noalias dereferenceable_or_null(40) i8* @malloc(i64 noundef 40)
103 %1 = bitcast i8* %call2 to i32*
104 %call3 = call noalias dereferenceable_or_null(40) i8* @malloc(i64 noundef 40)
105 %2 = bitcast i8* %call3 to i32*
106 %call4 = call noalias dereferenceable_or_null(40) i8* @malloc(i64 noundef 40)
107 %3 = bitcast i8* %call4 to i32*
108 ret i32* %1
109 }
110 declare dso_local noalias noundef i8* @malloc(i64 noundef)
111 )IR");
112
113 Function *Func = M->getFunction("test");
114
115 // First call has all cold contexts.
116 CallStackTrie Trie1;
117 Trie1.addCallStack(AllocationType::Cold, {1, 2});
118 Trie1.addCallStack(AllocationType::Cold, {1, 3, 4});
119 CallBase *Call1 = findCall(*Func, "call1");
120 Trie1.buildAndAttachMIBMetadata(Call1);
121
122 EXPECT_FALSE(Call1->hasMetadata(LLVMContext::MD_memprof));
123 EXPECT_TRUE(Call1->hasFnAttr("memprof"));
124 EXPECT_EQ(Call1->getFnAttr("memprof").getValueAsString(), "cold");
125
126 // Second call has all non-cold contexts.
127 CallStackTrie Trie2;
128 Trie2.addCallStack(AllocationType::NotCold, {5, 6});
129 Trie2.addCallStack(AllocationType::NotCold, {5, 7, 8});
130 CallBase *Call2 = findCall(*Func, "call2");
131 Trie2.buildAndAttachMIBMetadata(Call2);
132
133 EXPECT_FALSE(Call2->hasMetadata(LLVMContext::MD_memprof));
134 EXPECT_TRUE(Call2->hasFnAttr("memprof"));
135 EXPECT_EQ(Call2->getFnAttr("memprof").getValueAsString(), "notcold");
136
137 // Third call has all hot contexts.
138 CallStackTrie Trie3;
139 Trie3.addCallStack(AllocationType::Hot, {9, 10});
140 Trie3.addCallStack(AllocationType::Hot, {9, 11, 12});
141 CallBase *Call3 = findCall(*Func, "call3");
142 Trie3.buildAndAttachMIBMetadata(Call3);
143
144 EXPECT_FALSE(Call3->hasMetadata(LLVMContext::MD_memprof));
145 EXPECT_TRUE(Call3->hasFnAttr("memprof"));
146 EXPECT_EQ(Call3->getFnAttr("memprof").getValueAsString(), "hot");
147
148 // Fourth call has hot and non-cold contexts. These should be treated as
149 // notcold and given a notcold attribute.
150 CallStackTrie Trie4;
151 Trie4.addCallStack(AllocationType::Hot, {5, 6});
152 Trie4.addCallStack(AllocationType::NotCold, {5, 7, 8});
153 CallBase *Call4 = findCall(*Func, "call4");
154 Trie4.buildAndAttachMIBMetadata(Call4);
155
156 EXPECT_FALSE(Call4->hasMetadata(LLVMContext::MD_memprof));
157 EXPECT_TRUE(Call4->hasFnAttr("memprof"));
158 EXPECT_EQ(Call4->getFnAttr("memprof").getValueAsString(), "notcold");
159 }
160
161 // TODO: Use this matcher in existing tests.
162 // ExpectedVals should be a vector of expected MIBs and their allocation type
163 // and stack id contents in order, of type:
164 // std::vector<std::pair<AllocationType, std::vector<unsigned>>>
165 MATCHER_P(MemprofMetadataEquals, ExpectedVals, "Matching !memprof contents") {
166 auto PrintAndFail = [&]() {
167 std::string Buffer;
168 llvm::raw_string_ostream OS(Buffer);
169 OS << "Expected:\n";
170 for (auto &[ExpectedAllocType, ExpectedStackIds] : ExpectedVals) {
171 OS << "\t" << getAllocTypeAttributeString(ExpectedAllocType) << " { ";
172 for (auto Id : ExpectedStackIds)
173 OS << Id << " ";
174 OS << "}\n";
175 }
176 OS << "Got:\n";
177 arg->printTree(OS);
178 *result_listener << "!memprof metadata differs!\n" << Buffer;
179 return false;
180 };
181
182 if (ExpectedVals.size() != arg->getNumOperands())
183 return PrintAndFail();
184
185 for (size_t I = 0; I < ExpectedVals.size(); I++) {
186 const auto &[ExpectedAllocType, ExpectedStackIds] = ExpectedVals[I];
187 MDNode *MIB = dyn_cast<MDNode>(arg->getOperand(I));
188 if (getMIBAllocType(MIB) != ExpectedAllocType)
189 return PrintAndFail();
190 MDNode *StackMD = getMIBStackNode(MIB);
191 EXPECT_NE(StackMD, nullptr);
192 if (StackMD->getNumOperands() != ExpectedStackIds.size())
193 return PrintAndFail();
194 for (size_t J = 0; J < ExpectedStackIds.size(); J++) {
195 auto *StackId = mdconst::dyn_extract<ConstantInt>(StackMD->getOperand(J));
196 if (StackId->getZExtValue() != ExpectedStackIds[J])
197 return PrintAndFail();
198 }
199 }
200 return true;
201 }
202
203 // Test CallStackTrie::addCallStack interface taking allocation type and list of
204 // call stack ids.
205 // Test that an allocation call reached by both cold and non cold call stacks
206 // gets memprof metadata representing the different allocation type contexts.
207 TEST_F(MemoryProfileInfoTest, ColdAndNotColdMIB) {
208 LLVMContext C;
209 std::unique_ptr<Module> M = makeLLVMModule(C,
210 R"IR(
211 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
212 target triple = "x86_64-pc-linux-gnu"
213 define i32* @test() {
214 entry:
215 %call = call noalias dereferenceable_or_null(40) i8* @malloc(i64 noundef 40)
216 %0 = bitcast i8* %call to i32*
217 ret i32* %0
218 }
219 declare dso_local noalias noundef i8* @malloc(i64 noundef)
220 )IR");
221
222 Function *Func = M->getFunction("test");
223
224 CallStackTrie Trie;
225 Trie.addCallStack(AllocationType::Cold, {1, 2});
226 Trie.addCallStack(AllocationType::NotCold, {1, 3});
227
228 CallBase *Call = findCall(*Func, "call");
229 Trie.buildAndAttachMIBMetadata(Call);
230
231 EXPECT_FALSE(Call->hasFnAttr("memprof"));
232 EXPECT_TRUE(Call->hasMetadata(LLVMContext::MD_memprof));
233 MDNode *MemProfMD = Call->getMetadata(LLVMContext::MD_memprof);
234 ASSERT_EQ(MemProfMD->getNumOperands(), 2u);
235 for (auto &MIBOp : MemProfMD->operands()) {
236 MDNode *MIB = dyn_cast<MDNode>(MIBOp);
237 MDNode *StackMD = getMIBStackNode(MIB);
238 ASSERT_NE(StackMD, nullptr);
239 ASSERT_EQ(StackMD->getNumOperands(), 2u);
240 auto *StackId = mdconst::dyn_extract<ConstantInt>(StackMD->getOperand(0));
241 ASSERT_EQ(StackId->getZExtValue(), 1u);
242 StackId = mdconst::dyn_extract<ConstantInt>(StackMD->getOperand(1));
243 if (StackId->getZExtValue() == 2u)
244 EXPECT_EQ(getMIBAllocType(MIB), AllocationType::Cold);
245 else {
246 ASSERT_EQ(StackId->getZExtValue(), 3u);
247 EXPECT_EQ(getMIBAllocType(MIB), AllocationType::NotCold);
248 }
249 }
250 }
251
252 // Test CallStackTrie::addCallStack interface taking allocation type and list of
253 // call stack ids.
254 // Test that an allocation call reached by both cold and hot call stacks
255 // gets memprof metadata representing the different allocation type contexts.
256 TEST_F(MemoryProfileInfoTest, ColdAndHotMIB) {
257 LLVMContext C;
258 std::unique_ptr<Module> M = makeLLVMModule(C,
259 R"IR(
260 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
261 target triple = "x86_64-pc-linux-gnu"
262 define i32* @test() {
263 entry:
264 %call = call noalias dereferenceable_or_null(40) i8* @malloc(i64 noundef 40)
265 %0 = bitcast i8* %call to i32*
266 ret i32* %0
267 }
268 declare dso_local noalias noundef i8* @malloc(i64 noundef)
269 )IR");
270
271 Function *Func = M->getFunction("test");
272
273 CallStackTrie Trie;
274 Trie.addCallStack(AllocationType::Cold, {1, 2});
275 Trie.addCallStack(AllocationType::Hot, {1, 3});
276
277 CallBase *Call = findCall(*Func, "call");
278 Trie.buildAndAttachMIBMetadata(Call);
279
280 EXPECT_FALSE(Call->hasFnAttr("memprof"));
281 EXPECT_TRUE(Call->hasMetadata(LLVMContext::MD_memprof));
282 MDNode *MemProfMD = Call->getMetadata(LLVMContext::MD_memprof);
283 ASSERT_EQ(MemProfMD->getNumOperands(), 2u);
284 for (auto &MIBOp : MemProfMD->operands()) {
285 MDNode *MIB = dyn_cast<MDNode>(MIBOp);
286 MDNode *StackMD = getMIBStackNode(MIB);
287 ASSERT_NE(StackMD, nullptr);
288 ASSERT_EQ(StackMD->getNumOperands(), 2u);
289 auto *StackId = mdconst::dyn_extract<ConstantInt>(StackMD->getOperand(0));
290 ASSERT_EQ(StackId->getZExtValue(), 1u);
291 StackId = mdconst::dyn_extract<ConstantInt>(StackMD->getOperand(1));
292 if (StackId->getZExtValue() == 2u)
293 EXPECT_EQ(getMIBAllocType(MIB), AllocationType::Cold);
294 else {
295 ASSERT_EQ(StackId->getZExtValue(), 3u);
296 // Hot contexts are converted to NotCold when building the metadata.
297 EXPECT_EQ(getMIBAllocType(MIB), AllocationType::NotCold);
298 }
299 }
300 }
301
302 // Test CallStackTrie::addCallStack interface taking allocation type and list of
303 // call stack ids.
304 // Test that an allocation call reached by both cold, non cold and hot call
305 // stacks gets memprof metadata representing the different allocation type
306 // contexts.
307 TEST_F(MemoryProfileInfoTest, ColdAndNotColdAndHotMIB) {
308 LLVMContext C;
309 std::unique_ptr<Module> M = makeLLVMModule(C,
310 R"IR(
311 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
312 target triple = "x86_64-pc-linux-gnu"
313 define i32* @test() {
314 entry:
315 %call = call noalias dereferenceable_or_null(40) i8* @malloc(i64 noundef 40)
316 %0 = bitcast i8* %call to i32*
317 ret i32* %0
318 }
319 declare dso_local noalias noundef i8* @malloc(i64 noundef)
320 )IR");
321
322 Function *Func = M->getFunction("test");
323
324 CallStackTrie Trie;
325 Trie.addCallStack(AllocationType::Cold, {1, 2});
326 Trie.addCallStack(AllocationType::NotCold, {1, 3});
327 // This will be pruned as it is unnecessary to determine how to clone the
328 // cold allocation.
329 Trie.addCallStack(AllocationType::Hot, {1, 4});
330
331 CallBase *Call = findCall(*Func, "call");
332 Trie.buildAndAttachMIBMetadata(Call);
333
334 EXPECT_FALSE(Call->hasFnAttr("memprof"));
335 EXPECT_TRUE(Call->hasMetadata(LLVMContext::MD_memprof));
336 MDNode *MemProfMD = Call->getMetadata(LLVMContext::MD_memprof);
337 ASSERT_EQ(MemProfMD->getNumOperands(), 2u);
338 for (auto &MIBOp : MemProfMD->operands()) {
339 MDNode *MIB = dyn_cast<MDNode>(MIBOp);
340 MDNode *StackMD = getMIBStackNode(MIB);
341 ASSERT_NE(StackMD, nullptr);
342 ASSERT_EQ(StackMD->getNumOperands(), 2u);
343 auto *StackId = mdconst::dyn_extract<ConstantInt>(StackMD->getOperand(0));
344 ASSERT_EQ(StackId->getZExtValue(), 1u);
345 StackId = mdconst::dyn_extract<ConstantInt>(StackMD->getOperand(1));
346 if (StackId->getZExtValue() == 2u) {
347 EXPECT_EQ(getMIBAllocType(MIB), AllocationType::Cold);
348 } else if (StackId->getZExtValue() == 3u) {
349 EXPECT_EQ(getMIBAllocType(MIB), AllocationType::NotCold);
350 }
351 }
352 }
353
354 // Test CallStackTrie::addCallStack interface taking allocation type and list of
355 // call stack ids.
356 // Test that an allocation call reached by multiple call stacks has memprof
357 // metadata with the contexts trimmed to the minimum context required to
358 // identify the allocation type.
359 TEST_F(MemoryProfileInfoTest, TrimmedMIBContext) {
360 LLVMContext C;
361 std::unique_ptr<Module> M = makeLLVMModule(C,
362 R"IR(
363 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
364 target triple = "x86_64-pc-linux-gnu"
365 define i32* @test() {
366 entry:
367 %call = call noalias dereferenceable_or_null(40) i8* @malloc(i64 noundef 40)
368 %0 = bitcast i8* %call to i32*
369 ret i32* %0
370 }
371 declare dso_local noalias noundef i8* @malloc(i64 noundef)
372 )IR");
373
374 Function *Func = M->getFunction("test");
375
376 CallStackTrie Trie;
377 // We should be able to trim the following two and combine into a single MIB
378 // with the cold context {1, 2}.
379 Trie.addCallStack(AllocationType::Cold, {1, 2, 3});
380 Trie.addCallStack(AllocationType::Cold, {1, 2, 4});
381 // We should be able to trim the following two and combine into a single MIB
382 // with the non-cold context {1, 5}.
383 Trie.addCallStack(AllocationType::NotCold, {1, 5, 6});
384 Trie.addCallStack(AllocationType::NotCold, {1, 5, 7});
385 // These will be pruned as they are unnecessary to determine how to clone the
386 // cold allocation.
387 Trie.addCallStack(AllocationType::Hot, {1, 8, 9});
388 Trie.addCallStack(AllocationType::Hot, {1, 8, 10});
389
390 CallBase *Call = findCall(*Func, "call");
391 Trie.buildAndAttachMIBMetadata(Call);
392
393 EXPECT_FALSE(Call->hasFnAttr("memprof"));
394 EXPECT_TRUE(Call->hasMetadata(LLVMContext::MD_memprof));
395 MDNode *MemProfMD = Call->getMetadata(LLVMContext::MD_memprof);
396 ASSERT_EQ(MemProfMD->getNumOperands(), 2u);
397 for (auto &MIBOp : MemProfMD->operands()) {
398 MDNode *MIB = dyn_cast<MDNode>(MIBOp);
399 MDNode *StackMD = getMIBStackNode(MIB);
400 ASSERT_NE(StackMD, nullptr);
401 ASSERT_EQ(StackMD->getNumOperands(), 2u);
402 auto *StackId = mdconst::dyn_extract<ConstantInt>(StackMD->getOperand(0));
403 EXPECT_EQ(StackId->getZExtValue(), 1u);
404 StackId = mdconst::dyn_extract<ConstantInt>(StackMD->getOperand(1));
405 if (StackId->getZExtValue() == 2u)
406 EXPECT_EQ(getMIBAllocType(MIB), AllocationType::Cold);
407 else if (StackId->getZExtValue() == 5u)
408 EXPECT_EQ(getMIBAllocType(MIB), AllocationType::NotCold);
409 }
410 }
411
412 // Test to ensure that we prune NotCold contexts that are unneeded for
413 // determining where Cold contexts need to be cloned to enable correct hinting.
414 TEST_F(MemoryProfileInfoTest, PruneUnneededNotColdContexts) {
415 LLVMContext C;
416 std::unique_ptr<Module> M = makeLLVMModule(C,
417 R"IR(
418 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
419 target triple = "x86_64-pc-linux-gnu"
420 define i32* @test() {
421 entry:
422 %call = call noalias dereferenceable_or_null(40) i8* @malloc(i64 noundef 40)
423 %0 = bitcast i8* %call to i32*
424 ret i32* %0
425 }
426 declare dso_local noalias noundef i8* @malloc(i64 noundef)
427 )IR");
428
429 Function *Func = M->getFunction("test");
430
431 CallStackTrie Trie;
432
433 Trie.addCallStack(AllocationType::Cold, {1, 2, 3, 4});
434 Trie.addCallStack(AllocationType::Cold, {1, 2, 3, 5, 6, 7});
435 // This NotCold context is needed to know where the above two Cold contexts
436 // must be cloned from:
437 Trie.addCallStack(AllocationType::NotCold, {1, 2, 3, 5, 6, 13});
438
439 Trie.addCallStack(AllocationType::Cold, {1, 2, 3, 8, 9, 10});
440 // This NotCold context is needed to know where the above Cold context must be
441 // cloned from:
442 Trie.addCallStack(AllocationType::NotCold, {1, 2, 3, 8, 9, 14});
443 // This NotCold context is not needed since the above is sufficient (we pick
444 // the first in sorted order).
445 Trie.addCallStack(AllocationType::NotCold, {1, 2, 3, 8, 9, 15});
446
447 // None of these NotCold contexts are needed as the Cold contexts they
448 // overlap with are covered by longer overlapping NotCold contexts.
449 Trie.addCallStack(AllocationType::NotCold, {1, 2, 3, 12});
450 Trie.addCallStack(AllocationType::NotCold, {1, 2, 11});
451 Trie.addCallStack(AllocationType::NotCold, {1, 16});
452
453 std::vector<std::pair<AllocationType, std::vector<unsigned>>> ExpectedVals = {
454 {AllocationType::Cold, {1, 2, 3, 4}},
455 {AllocationType::Cold, {1, 2, 3, 5, 6, 7}},
456 {AllocationType::NotCold, {1, 2, 3, 5, 6, 13}},
457 {AllocationType::Cold, {1, 2, 3, 8, 9, 10}},
458 {AllocationType::NotCold, {1, 2, 3, 8, 9, 14}}};
459
460 CallBase *Call = findCall(*Func, "call");
461 ASSERT_NE(Call, nullptr);
462 Trie.buildAndAttachMIBMetadata(Call);
463
464 EXPECT_FALSE(Call->hasFnAttr("memprof"));
465 EXPECT_TRUE(Call->hasMetadata(LLVMContext::MD_memprof));
466 MDNode *MemProfMD = Call->getMetadata(LLVMContext::MD_memprof);
467 EXPECT_THAT(MemProfMD, MemprofMetadataEquals(ExpectedVals));
468 }
469
470 // Test to ensure that we keep optionally keep unneeded NotCold contexts.
471 // Same as PruneUnneededNotColdContexts test but with the
472 // MemProfKeepAllNotColdContexts set to true.
473 TEST_F(MemoryProfileInfoTest, KeepUnneededNotColdContexts) {
474 LLVMContext C;
475 std::unique_ptr<Module> M = makeLLVMModule(C,
476 R"IR(
477 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
478 target triple = "x86_64-pc-linux-gnu"
479 define i32* @test() {
480 entry:
481 %call = call noalias dereferenceable_or_null(40) i8* @malloc(i64 noundef 40)
482 %0 = bitcast i8* %call to i32*
483 ret i32* %0
484 }
485 declare dso_local noalias noundef i8* @malloc(i64 noundef)
486 )IR");
487
488 Function *Func = M->getFunction("test");
489
490 CallStackTrie Trie;
491
492 Trie.addCallStack(AllocationType::Cold, {1, 2, 3, 4});
493 Trie.addCallStack(AllocationType::Cold, {1, 2, 3, 5, 6, 7});
494 // This NotCold context is needed to know where the above two Cold contexts
495 // must be cloned from:
496 Trie.addCallStack(AllocationType::NotCold, {1, 2, 3, 5, 6, 13});
497
498 Trie.addCallStack(AllocationType::Cold, {1, 2, 3, 8, 9, 10});
499 // This NotCold context is needed to know where the above Cold context must be
500 // cloned from:
501 Trie.addCallStack(AllocationType::NotCold, {1, 2, 3, 8, 9, 14});
502 // This NotCold context is not needed since the above is sufficient (we pick
503 // the first in sorted order).
504 Trie.addCallStack(AllocationType::NotCold, {1, 2, 3, 8, 9, 15});
505
506 // None of these NotCold contexts are needed as the Cold contexts they
507 // overlap with are covered by longer overlapping NotCold contexts.
508 Trie.addCallStack(AllocationType::NotCold, {1, 2, 3, 12});
509 Trie.addCallStack(AllocationType::NotCold, {1, 2, 11});
510 Trie.addCallStack(AllocationType::NotCold, {1, 16});
511
512 // We should keep all of the above contexts, even those that are unneeded by
513 // default, because we will set the option to keep them.
514 std::vector<std::pair<AllocationType, std::vector<unsigned>>> ExpectedVals = {
515 {AllocationType::Cold, {1, 2, 3, 4}},
516 {AllocationType::Cold, {1, 2, 3, 5, 6, 7}},
517 {AllocationType::NotCold, {1, 2, 3, 5, 6, 13}},
518 {AllocationType::Cold, {1, 2, 3, 8, 9, 10}},
519 {AllocationType::NotCold, {1, 2, 3, 8, 9, 14}},
520 {AllocationType::NotCold, {1, 2, 3, 8, 9, 15}},
521 {AllocationType::NotCold, {1, 2, 3, 12}},
522 {AllocationType::NotCold, {1, 2, 11}},
523 {AllocationType::NotCold, {1, 16}}};
524
525 CallBase *Call = findCall(*Func, "call");
526 ASSERT_NE(Call, nullptr);
527
528 // Specify that all non-cold contexts should be kept.
529 bool OrigMemProfKeepAllNotColdContexts = MemProfKeepAllNotColdContexts;
530 MemProfKeepAllNotColdContexts = true;
531
532 Trie.buildAndAttachMIBMetadata(Call);
533
534 // Restore original option value.
535 MemProfKeepAllNotColdContexts = OrigMemProfKeepAllNotColdContexts;
536
537 EXPECT_FALSE(Call->hasFnAttr("memprof"));
538 EXPECT_TRUE(Call->hasMetadata(LLVMContext::MD_memprof));
539 MDNode *MemProfMD = Call->getMetadata(LLVMContext::MD_memprof);
540 EXPECT_THAT(MemProfMD, MemprofMetadataEquals(ExpectedVals));
541 }
542
543 // Test CallStackTrie::addCallStack interface taking memprof MIB metadata.
544 // Check that allocations annotated with memprof metadata with a single
545 // allocation type get simplified to an attribute.
546 TEST_F(MemoryProfileInfoTest, SimplifyMIBToAttribute) {
547 LLVMContext C;
548 std::unique_ptr<Module> M = makeLLVMModule(C,
549 R"IR(
550 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
551 target triple = "x86_64-pc-linux-gnu"
552 define i32* @test() {
553 entry:
554 %call1 = call noalias dereferenceable_or_null(40) i8* @malloc(i64 noundef 40), !memprof !0
555 %0 = bitcast i8* %call1 to i32*
556 %call2 = call noalias dereferenceable_or_null(40) i8* @malloc(i64 noundef 40), !memprof !3
557 %1 = bitcast i8* %call2 to i32*
558 %call3 = call noalias dereferenceable_or_null(40) i8* @malloc(i64 noundef 40), !memprof !6
559 %2 = bitcast i8* %call3 to i32*
560 ret i32* %1
561 }
562 declare dso_local noalias noundef i8* @malloc(i64 noundef)
563 !0 = !{!1}
564 !1 = !{!2, !"cold"}
565 !2 = !{i64 1, i64 2, i64 3}
566 !3 = !{!4}
567 !4 = !{!5, !"notcold"}
568 !5 = !{i64 4, i64 5, i64 6, i64 7}
569 !6 = !{!7}
570 !7 = !{!8, !"hot"}
571 !8 = !{i64 8, i64 9, i64 10}
572 )IR");
573
574 Function *Func = M->getFunction("test");
575
576 // First call has all cold contexts.
577 CallStackTrie Trie1;
578 CallBase *Call1 = findCall(*Func, "call1");
579 MDNode *MemProfMD1 = Call1->getMetadata(LLVMContext::MD_memprof);
580 ASSERT_EQ(MemProfMD1->getNumOperands(), 1u);
581 MDNode *MIB1 = dyn_cast<MDNode>(MemProfMD1->getOperand(0));
582 Trie1.addCallStack(MIB1);
583 Trie1.buildAndAttachMIBMetadata(Call1);
584
585 EXPECT_TRUE(Call1->hasFnAttr("memprof"));
586 EXPECT_EQ(Call1->getFnAttr("memprof").getValueAsString(), "cold");
587
588 // Second call has all non-cold contexts.
589 CallStackTrie Trie2;
590 CallBase *Call2 = findCall(*Func, "call2");
591 MDNode *MemProfMD2 = Call2->getMetadata(LLVMContext::MD_memprof);
592 ASSERT_EQ(MemProfMD2->getNumOperands(), 1u);
593 MDNode *MIB2 = dyn_cast<MDNode>(MemProfMD2->getOperand(0));
594 Trie2.addCallStack(MIB2);
595 Trie2.buildAndAttachMIBMetadata(Call2);
596
597 EXPECT_TRUE(Call2->hasFnAttr("memprof"));
598 EXPECT_EQ(Call2->getFnAttr("memprof").getValueAsString(), "notcold");
599
600 // Third call has all hot contexts.
601 CallStackTrie Trie3;
602 CallBase *Call3 = findCall(*Func, "call3");
603 MDNode *MemProfMD3 = Call3->getMetadata(LLVMContext::MD_memprof);
604 ASSERT_EQ(MemProfMD2->getNumOperands(), 1u);
605 MDNode *MIB3 = dyn_cast<MDNode>(MemProfMD3->getOperand(0));
606 Trie3.addCallStack(MIB3);
607 Trie3.buildAndAttachMIBMetadata(Call3);
608
609 EXPECT_TRUE(Call3->hasFnAttr("memprof"));
610 EXPECT_EQ(Call3->getFnAttr("memprof").getValueAsString(), "hot");
611 }
612
613 // Test CallStackTrie::addCallStack interface taking memprof MIB metadata.
614 // Test that allocations annotated with memprof metadata with multiple call
615 // stacks gets new memprof metadata with the contexts trimmed to the minimum
616 // context required to identify the allocation type.
617 TEST_F(MemoryProfileInfoTest, ReTrimMIBContext) {
618 LLVMContext C;
619 std::unique_ptr<Module> M = makeLLVMModule(C,
620 R"IR(
621 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
622 target triple = "x86_64-pc-linux-gnu"
623 define i32* @test() {
624 entry:
625 %call = call noalias dereferenceable_or_null(40) i8* @malloc(i64 noundef 40), !memprof !0
626 %0 = bitcast i8* %call to i32*
627 ret i32* %0
628 }
629 declare dso_local noalias noundef i8* @malloc(i64 noundef)
630 !0 = !{!1, !3, !5, !7, !9, !11}
631 !1 = !{!2, !"cold"}
632 !2 = !{i64 1, i64 2, i64 3}
633 !3 = !{!4, !"cold"}
634 !4 = !{i64 1, i64 2, i64 4}
635 !5 = !{!6, !"notcold"}
636 !6 = !{i64 1, i64 5, i64 6}
637 !7 = !{!8, !"notcold"}
638 !8 = !{i64 1, i64 5, i64 7}
639 !9 = !{!10, !"hot"}
640 !10 = !{i64 1, i64 8, i64 9}
641 !11 = !{!12, !"hot"}
642 !12 = !{i64 1, i64 8, i64 10}
643 )IR");
644
645 Function *Func = M->getFunction("test");
646
647 CallStackTrie Trie;
648 ASSERT_TRUE(Trie.empty());
649 CallBase *Call = findCall(*Func, "call");
650 MDNode *MemProfMD = Call->getMetadata(LLVMContext::MD_memprof);
651 for (auto &MIBOp : MemProfMD->operands()) {
652 MDNode *MIB = dyn_cast<MDNode>(MIBOp);
653 Trie.addCallStack(MIB);
654 }
655 ASSERT_FALSE(Trie.empty());
656 Trie.buildAndAttachMIBMetadata(Call);
657
658 // We should be able to trim the first two and combine into a single MIB
659 // with the cold context {1, 2}.
660 // We should be able to trim the second two and combine into a single MIB
661 // with the non-cold context {1, 5}.
662 // The hot allocations will be converted to NotCold and pruned as they
663 // are unnecessary to determine how to clone the cold allocation.
664
665 EXPECT_FALSE(Call->hasFnAttr("memprof"));
666 EXPECT_TRUE(Call->hasMetadata(LLVMContext::MD_memprof));
667 MemProfMD = Call->getMetadata(LLVMContext::MD_memprof);
668 ASSERT_EQ(MemProfMD->getNumOperands(), 2u);
669 for (auto &MIBOp : MemProfMD->operands()) {
670 MDNode *MIB = dyn_cast<MDNode>(MIBOp);
671 MDNode *StackMD = getMIBStackNode(MIB);
672 ASSERT_NE(StackMD, nullptr);
673 ASSERT_EQ(StackMD->getNumOperands(), 2u);
674 auto *StackId = mdconst::dyn_extract<ConstantInt>(StackMD->getOperand(0));
675 EXPECT_EQ(StackId->getZExtValue(), 1u);
676 StackId = mdconst::dyn_extract<ConstantInt>(StackMD->getOperand(1));
677 if (StackId->getZExtValue() == 2u)
678 EXPECT_EQ(getMIBAllocType(MIB), AllocationType::Cold);
679 else if (StackId->getZExtValue() == 5u)
680 EXPECT_EQ(getMIBAllocType(MIB), AllocationType::NotCold);
681 }
682 }
683
684 TEST_F(MemoryProfileInfoTest, CallStackTestIR) {
685 LLVMContext C;
686 std::unique_ptr<Module> M = makeLLVMModule(C,
687 R"IR(
688 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
689 target triple = "x86_64-pc-linux-gnu"
690 define ptr @test() {
691 entry:
692 %call = call noalias noundef nonnull dereferenceable(10) ptr @_Znam(i64 noundef 10), !memprof !1, !callsite !6
693 ret ptr %call
694 }
695 declare noundef nonnull ptr @_Znam(i64 noundef)
696 !1 = !{!2, !4, !7}
697 !2 = !{!3, !"notcold"}
698 !3 = !{i64 1, i64 2, i64 3, i64 4}
699 !4 = !{!5, !"cold"}
700 !5 = !{i64 1, i64 2, i64 3, i64 5}
701 !6 = !{i64 1}
702 !7 = !{!8, !"hot"}
703 !8 = !{i64 1, i64 2, i64 3, i64 6}
704 )IR");
705
706 Function *Func = M->getFunction("test");
707 CallBase *Call = findCall(*Func, "call");
708
709 CallStack<MDNode, MDNode::op_iterator> InstCallsite(
710 Call->getMetadata(LLVMContext::MD_callsite));
711
712 MDNode *MemProfMD = Call->getMetadata(LLVMContext::MD_memprof);
713 unsigned Idx = 0;
714 for (auto &MIBOp : MemProfMD->operands()) {
715 auto *MIBMD = cast<const MDNode>(MIBOp);
716 MDNode *StackNode = getMIBStackNode(MIBMD);
717 CallStack<MDNode, MDNode::op_iterator> StackContext(StackNode);
718 EXPECT_EQ(StackContext.back(), 4 + Idx);
719 std::vector<uint64_t> StackIds;
720 for (auto ContextIter = StackContext.beginAfterSharedPrefix(InstCallsite);
721 ContextIter != StackContext.end(); ++ContextIter)
722 StackIds.push_back(*ContextIter);
723 if (Idx == 0) {
724 std::vector<uint64_t> Expected = {2, 3, 4};
725 EXPECT_EQ(ArrayRef(StackIds), ArrayRef(Expected));
726 } else if (Idx == 1) {
727 std::vector<uint64_t> Expected = {2, 3, 5};
728 EXPECT_EQ(ArrayRef(StackIds), ArrayRef(Expected));
729 } else {
730 std::vector<uint64_t> Expected = {2, 3, 6};
731 EXPECT_EQ(ArrayRef(StackIds), ArrayRef(Expected));
732 }
733 Idx++;
734 }
735 }
736
737 TEST_F(MemoryProfileInfoTest, CallStackTestSummary) {
738 std::unique_ptr<ModuleSummaryIndex> Index = makeLLVMIndex(R"Summary(
739 ^0 = module: (path: "test.o", hash: (0, 0, 0, 0, 0))
740 ^1 = gv: (guid: 23, summaries: (function: (module: ^0, flags: (linkage: external, visibility: default, notEligibleToImport: 0, live: 0, dsoLocal: 1, canAutoHide: 0), insts: 2, funcFlags: (readNone: 0, readOnly: 0, noRecurse: 0, returnDoesNotAlias: 0, noInline: 1, alwaysInline: 0, noUnwind: 0, mayThrow: 0, hasUnknownCall: 0, mustBeUnreachable: 0), allocs: ((versions: (none), memProf: ((type: notcold, stackIds: (1, 2, 3, 4)), (type: cold, stackIds: (1, 2, 3, 5)), (type: hot, stackIds: (1, 2, 3, 6))))))))
741 ^2 = gv: (guid: 25, summaries: (function: (module: ^0, flags: (linkage: external, visibility: default, notEligibleToImport: 0, live: 0, dsoLocal: 1, canAutoHide: 0), insts: 22, funcFlags: (readNone: 0, readOnly: 0, noRecurse: 1, returnDoesNotAlias: 0, noInline: 1, alwaysInline: 0, noUnwind: 0, mayThrow: 0, hasUnknownCall: 0, mustBeUnreachable: 0), calls: ((callee: ^1)), callsites: ((callee: ^1, clones: (0), stackIds: (3, 4)), (callee: ^1, clones: (0), stackIds: (3, 5)), (callee: ^1, clones: (0), stackIds: (3, 6))))))
742 )Summary");
743
744 ASSERT_NE(Index, nullptr);
745 auto *CallsiteSummary =
746 cast<FunctionSummary>(Index->getGlobalValueSummary(/*guid=*/25));
747 unsigned Idx = 0;
748 for (auto &CI : CallsiteSummary->callsites()) {
749 CallStack<CallsiteInfo, SmallVector<unsigned>::const_iterator> InstCallsite(
750 &CI);
751 std::vector<uint64_t> StackIds;
752 for (auto StackIdIndex : InstCallsite)
753 StackIds.push_back(Index->getStackIdAtIndex(StackIdIndex));
754 if (Idx == 0) {
755 std::vector<uint64_t> Expected = {3, 4};
756 EXPECT_EQ(ArrayRef(StackIds), ArrayRef(Expected));
757 } else if (Idx == 1) {
758 std::vector<uint64_t> Expected = {3, 5};
759 EXPECT_EQ(ArrayRef(StackIds), ArrayRef(Expected));
760 } else {
761 std::vector<uint64_t> Expected = {3, 6};
762 EXPECT_EQ(ArrayRef(StackIds), ArrayRef(Expected));
763 }
764 Idx++;
765 }
766
767 auto *AllocSummary =
768 cast<FunctionSummary>(Index->getGlobalValueSummary(/*guid=*/23));
769 for (auto &AI : AllocSummary->allocs()) {
770 unsigned Idx = 0;
771 for (auto &MIB : AI.MIBs) {
772 CallStack<MIBInfo, SmallVector<unsigned>::const_iterator> StackContext(
773 &MIB);
774 EXPECT_EQ(Index->getStackIdAtIndex(StackContext.back()), 4 + Idx);
775 std::vector<uint64_t> StackIds;
776 for (auto StackIdIndex : StackContext)
777 StackIds.push_back(Index->getStackIdAtIndex(StackIdIndex));
778 if (Idx == 0) {
779 std::vector<uint64_t> Expected = {1, 2, 3, 4};
780 EXPECT_EQ(ArrayRef(StackIds), ArrayRef(Expected));
781 } else if (Idx == 1) {
782 std::vector<uint64_t> Expected = {1, 2, 3, 5};
783 EXPECT_EQ(ArrayRef(StackIds), ArrayRef(Expected));
784 } else {
785 std::vector<uint64_t> Expected = {1, 2, 3, 6};
786 EXPECT_EQ(ArrayRef(StackIds), ArrayRef(Expected));
787 }
788 Idx++;
789 }
790 }
791 }
792 } // end anonymous namespace