aboutsummaryrefslogtreecommitdiff
path: root/llvm/unittests/IR/ModuleTest.cpp
blob: 36c356730d27a76aeb28275f493d1948157f8988 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
//===- unittests/IR/ModuleTest.cpp - Module unit tests --------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "llvm/IR/Module.h"
#include "llvm/AsmParser/Parser.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/GlobalVariable.h"
#include "llvm/IR/ModuleSummaryIndex.h"
#include "llvm/Pass.h"
#include "llvm/Support/RandomNumberGenerator.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/raw_ostream.h"
#include "gtest/gtest.h"

#include <random>

using namespace llvm;

namespace {

bool sortByName(const GlobalVariable &L, const GlobalVariable &R) {
  return L.getName() < R.getName();
}

bool sortByNameReverse(const GlobalVariable &L, const GlobalVariable &R) {
  return sortByName(R, L);
}

TEST(ModuleTest, sortGlobalsByName) {
  LLVMContext Context;
  for (auto compare : {&sortByName, &sortByNameReverse}) {
    Module M("M", Context);
    Type *T = Type::getInt8Ty(Context);
    GlobalValue::LinkageTypes L = GlobalValue::ExternalLinkage;
    (void)new GlobalVariable(M, T, false, L, nullptr, "A");
    (void)new GlobalVariable(M, T, false, L, nullptr, "F");
    (void)new GlobalVariable(M, T, false, L, nullptr, "G");
    (void)new GlobalVariable(M, T, false, L, nullptr, "E");
    (void)new GlobalVariable(M, T, false, L, nullptr, "B");
    (void)new GlobalVariable(M, T, false, L, nullptr, "H");
    (void)new GlobalVariable(M, T, false, L, nullptr, "C");
    (void)new GlobalVariable(M, T, false, L, nullptr, "D");

    // Sort the globals by name.
    EXPECT_FALSE(std::is_sorted(M.global_begin(), M.global_end(), compare));
  }
}

TEST(ModuleTest, randomNumberGenerator) {
  LLVMContext Context;
  static char ID;
  struct DummyPass : ModulePass {
    DummyPass() : ModulePass(ID) {}
    bool runOnModule(Module &) override { return true; }
  } DP;

  Module M("R", Context);

  std::uniform_int_distribution<int> dist;
  const size_t NBCheck = 10;

  std::array<int, NBCheck> RandomStreams[2];
  for (auto &RandomStream : RandomStreams) {
    std::unique_ptr<RandomNumberGenerator> RNG = M.createRNG(DP.getPassName());
    std::generate(RandomStream.begin(), RandomStream.end(),
                  [&]() { return dist(*RNG); });
  }

  EXPECT_TRUE(std::equal(RandomStreams[0].begin(), RandomStreams[0].end(),
                         RandomStreams[1].begin()));
}

TEST(ModuleTest, setModuleFlag) {
  LLVMContext Context;
  Module M("M", Context);
  StringRef Key = "Key";
  Metadata *Val1 = MDString::get(Context, "Val1");
  Metadata *Val2 = MDString::get(Context, "Val2");
  EXPECT_EQ(nullptr, M.getModuleFlag(Key));
  M.setModuleFlag(Module::ModFlagBehavior::Error, Key, Val1);
  EXPECT_EQ(Val1, M.getModuleFlag(Key));
  M.setModuleFlag(Module::ModFlagBehavior::Error, Key, Val2);
  EXPECT_EQ(Val2, M.getModuleFlag(Key));
}

TEST(ModuleTest, setModuleFlagInt) {
  LLVMContext Context;
  Module M("M", Context);
  StringRef Key = "Key";
  uint32_t Val1 = 1;
  uint32_t Val2 = 2;
  EXPECT_EQ(nullptr, M.getModuleFlag(Key));
  M.setModuleFlag(Module::ModFlagBehavior::Error, Key, Val1);
  auto A1 = mdconst::extract_or_null<ConstantInt>(M.getModuleFlag(Key));
  EXPECT_EQ(Val1, A1->getZExtValue());
  M.setModuleFlag(Module::ModFlagBehavior::Error, Key, Val2);
  auto A2 = mdconst::extract_or_null<ConstantInt>(M.getModuleFlag(Key));
  EXPECT_EQ(Val2, A2->getZExtValue());
}

const char *IRString = R"IR(
  !llvm.module.flags = !{!0}

  !0 = !{i32 1, !"ProfileSummary", !1}
  !1 = !{!2, !3, !4, !5, !6, !7, !8, !9}
  !2 = !{!"ProfileFormat", !"SampleProfile"}
  !3 = !{!"TotalCount", i64 10000}
  !4 = !{!"MaxCount", i64 10}
  !5 = !{!"MaxInternalCount", i64 1}
  !6 = !{!"MaxFunctionCount", i64 1000}
  !7 = !{!"NumCounts", i64 200}
  !8 = !{!"NumFunctions", i64 3}
  !9 = !{!"DetailedSummary", !10}
  !10 = !{!11, !12, !13}
  !11 = !{i32 10000, i64 1000, i32 1}
  !12 = !{i32 990000, i64 300, i32 10}
  !13 = !{i32 999999, i64 5, i32 100}
)IR";

TEST(ModuleTest, setProfileSummary) {
  SMDiagnostic Err;
  LLVMContext Context;
  std::unique_ptr<Module> M = parseAssemblyString(IRString, Err, Context);
  auto *PS = ProfileSummary::getFromMD(M->getProfileSummary(/*IsCS*/ false));
  EXPECT_NE(nullptr, PS);
  EXPECT_FALSE(PS->isPartialProfile());
  PS->setPartialProfile(true);
  M->setProfileSummary(PS->getMD(Context), ProfileSummary::PSK_Sample);
  delete PS;
  PS = ProfileSummary::getFromMD(M->getProfileSummary(/*IsCS*/ false));
  EXPECT_NE(nullptr, PS);
  EXPECT_EQ(true, PS->isPartialProfile());
  delete PS;
}

TEST(ModuleTest, setPartialSampleProfileRatio) {
  const char *IRString = R"IR(
  !llvm.module.flags = !{!0}

  !0 = !{i32 1, !"ProfileSummary", !1}
  !1 = !{!2, !3, !4, !5, !6, !7, !8, !9, !10, !11}
  !2 = !{!"ProfileFormat", !"SampleProfile"}
  !3 = !{!"TotalCount", i64 10000}
  !4 = !{!"MaxCount", i64 10}
  !5 = !{!"MaxInternalCount", i64 1}
  !6 = !{!"MaxFunctionCount", i64 1000}
  !7 = !{!"NumCounts", i64 200}
  !8 = !{!"NumFunctions", i64 3}
  !9 = !{!"IsPartialProfile", i64 1}
  !10 = !{!"PartialProfileRatio", double 0.0}
  !11 = !{!"DetailedSummary", !12}
  !12 = !{!13, !14, !15}
  !13 = !{i32 10000, i64 1000, i32 1}
  !14 = !{i32 990000, i64 300, i32 10}
  !15 = !{i32 999999, i64 5, i32 100}
  )IR";

  SMDiagnostic Err;
  LLVMContext Context;
  std::unique_ptr<Module> M = parseAssemblyString(IRString, Err, Context);
  ModuleSummaryIndex Index(/*HaveGVs*/ false);
  const unsigned BlockCount = 100;
  const unsigned NumCounts = 200;
  Index.setBlockCount(BlockCount);
  M->setPartialSampleProfileRatio(Index);
  double Ratio = (double)BlockCount / NumCounts;
  std::unique_ptr<ProfileSummary> ProfileSummary(
      ProfileSummary::getFromMD(M->getProfileSummary(/*IsCS*/ false)));
  EXPECT_EQ(Ratio, ProfileSummary->getPartialProfileRatio());
}

TEST(ModuleTest, AliasList) {
  // This tests all Module's functions that interact with Module::AliasList.
  LLVMContext C;
  SMDiagnostic Err;
  LLVMContext Context;
  std::unique_ptr<Module> M = parseAssemblyString(R"(
declare void @Foo()
@GA = alias void (), ptr @Foo
)",
                                                  Err, Context);
  Function *Foo = M->getFunction("Foo");
  auto *GA = M->getNamedAlias("GA");
  EXPECT_EQ(M->alias_size(), 1u);
  auto *NewGA =
      GlobalAlias::create(Foo->getType(), 0, GlobalValue::ExternalLinkage,
                          "NewGA", Foo, /*Parent=*/nullptr);
  EXPECT_EQ(M->alias_size(), 1u);

  M->insertAlias(NewGA);
  EXPECT_EQ(&*std::prev(M->aliases().end()), NewGA);

  M->removeAlias(NewGA);
  EXPECT_EQ(M->alias_size(), 1u);
  M->insertAlias(NewGA);
  EXPECT_EQ(M->alias_size(), 2u);
  EXPECT_EQ(&*std::prev(M->aliases().end()), NewGA);

  auto Range = M->aliases();
  EXPECT_EQ(&*Range.begin(), GA);
  EXPECT_EQ(&*std::next(Range.begin()), NewGA);
  EXPECT_EQ(std::next(Range.begin(), 2), Range.end());

  M->removeAlias(NewGA);
  EXPECT_EQ(M->alias_size(), 1u);

  M->insertAlias(NewGA);
  M->eraseAlias(NewGA);
  EXPECT_EQ(M->alias_size(), 1u);
}

TEST(ModuleTest, IFuncList) {
  // This tests all Module's functions that interact with Module::IFuncList.
  LLVMContext C;
  SMDiagnostic Err;
  LLVMContext Context;
  std::unique_ptr<Module> M = parseAssemblyString(R"(
declare void @Foo()
@GIF = ifunc void (), ptr @Foo
)",
                                                  Err, Context);
  Function *Foo = M->getFunction("Foo");
  auto *GIF = M->getNamedIFunc("GIF");
  EXPECT_EQ(M->ifunc_size(), 1u);
  auto *NewGIF =
      GlobalIFunc::create(Foo->getType(), 0, GlobalValue::ExternalLinkage,
                          "NewGIF", Foo, /*Parent=*/nullptr);
  EXPECT_EQ(M->ifunc_size(), 1u);

  M->insertIFunc(NewGIF);
  EXPECT_EQ(&*std::prev(M->ifuncs().end()), NewGIF);

  M->removeIFunc(NewGIF);
  EXPECT_EQ(M->ifunc_size(), 1u);
  M->insertIFunc(NewGIF);
  EXPECT_EQ(M->ifunc_size(), 2u);
  EXPECT_EQ(&*std::prev(M->ifuncs().end()), NewGIF);

  auto Range = M->ifuncs();
  EXPECT_EQ(&*Range.begin(), GIF);
  EXPECT_EQ(&*std::next(Range.begin()), NewGIF);
  EXPECT_EQ(std::next(Range.begin(), 2), Range.end());

  M->removeIFunc(NewGIF);
  EXPECT_EQ(M->ifunc_size(), 1u);

  M->insertIFunc(NewGIF);
  M->eraseIFunc(NewGIF);
  EXPECT_EQ(M->ifunc_size(), 1u);
}

TEST(ModuleTest, NamedMDList) {
  // This tests all Module's functions that interact with Module::NamedMDList.
  LLVMContext C;
  SMDiagnostic Err;
  LLVMContext Context;
  auto M = std::make_unique<Module>("M", C);
  NamedMDNode *MDN1 = M->getOrInsertNamedMetadata("MDN1");
  EXPECT_EQ(M->named_metadata_size(), 1u);
  NamedMDNode *MDN2 = M->getOrInsertNamedMetadata("MDN2");
  EXPECT_EQ(M->named_metadata_size(), 2u);
  auto *NewMDN = M->getOrInsertNamedMetadata("NewMDN");
  EXPECT_EQ(M->named_metadata_size(), 3u);

  M->removeNamedMDNode(NewMDN);
  EXPECT_EQ(M->named_metadata_size(), 2u);

  M->insertNamedMDNode(NewMDN);
  EXPECT_EQ(&*std::prev(M->named_metadata().end()), NewMDN);

  M->removeNamedMDNode(NewMDN);
  M->insertNamedMDNode(NewMDN);
  EXPECT_EQ(M->named_metadata_size(), 3u);
  EXPECT_EQ(&*std::prev(M->named_metadata().end()), NewMDN);

  auto Range = M->named_metadata();
  EXPECT_EQ(&*Range.begin(), MDN1);
  EXPECT_EQ(&*std::next(Range.begin(), 1), MDN2);
  EXPECT_EQ(&*std::next(Range.begin(), 2), NewMDN);
  EXPECT_EQ(std::next(Range.begin(), 3), Range.end());

  M->eraseNamedMDNode(NewMDN);
  EXPECT_EQ(M->named_metadata_size(), 2u);
}

TEST(ModuleTest, GlobalList) {
  // This tests all Module's functions that interact with Module::GlobalList.
  LLVMContext C;
  SMDiagnostic Err;
  LLVMContext Context;
  std::unique_ptr<Module> M = parseAssemblyString(R"(
@GV = external global i32
)",
                                                  Err, Context);
  auto *GV = cast<GlobalVariable>(M->getNamedValue("GV"));
  EXPECT_EQ(M->global_size(), 1u);
  GlobalVariable *NewGV = new GlobalVariable(
      Type::getInt32Ty(C), /*isConstant=*/true, GlobalValue::InternalLinkage,
      /*Initializer=*/nullptr, "NewGV");
  EXPECT_EQ(M->global_size(), 1u);
  // Insert before
  M->insertGlobalVariable(M->globals().begin(), NewGV);
  EXPECT_EQ(M->global_size(), 2u);
  EXPECT_EQ(&*M->globals().begin(), NewGV);
  // Insert at end()
  M->removeGlobalVariable(NewGV);
  EXPECT_EQ(M->global_size(), 1u);
  M->insertGlobalVariable(NewGV);
  EXPECT_EQ(M->global_size(), 2u);
  EXPECT_EQ(&*std::prev(M->globals().end()), NewGV);
  // Check globals()
  auto Range = M->globals();
  EXPECT_EQ(&*Range.begin(), GV);
  EXPECT_EQ(&*std::next(Range.begin()), NewGV);
  EXPECT_EQ(std::next(Range.begin(), 2), Range.end());
  // Check remove
  M->removeGlobalVariable(NewGV);
  EXPECT_EQ(M->global_size(), 1u);
  // Check erase
  M->insertGlobalVariable(NewGV);
  M->eraseGlobalVariable(NewGV);
  EXPECT_EQ(M->global_size(), 1u);
}

TEST(ModuleTest, MoveAssign) {
  // This tests that we can move-assign modules, we parse two modules and
  // move assign the second one to the first one, and check that the print
  // is equal to what we loaded.
  LLVMContext C;
  SMDiagnostic Err;
  LLVMContext Context;
  std::unique_ptr<Module> M1 = parseAssemblyString(R"(
; ModuleID = '<string>'
source_filename = "<string1>"

@GV1 = external global i32

@GA1 = alias void (), ptr @Foo1

define void @Foo1() {
  ret void
}

!llvm.module.flags = !{!0}
!llvm.dbg.cu = !{!1}
!foo1 = !{!3}
!bar1 = !{!4}

!0 = !{i32 2, !"Debug Info Version", i32 3}
!1 = distinct !DICompileUnit(language: DW_LANG_C99, file: !2, producer: "clang1", isOptimized: true, flags: "-O2", runtimeVersion: 0, splitDebugFilename: "abc.debug", emissionKind: LineTablesOnly)
!2 = !DIFile(filename: "path/to/file1", directory: "/path/to/dir1")
!3 = !DILocation(line: 12, column: 34, scope: !4)
!4 = distinct !DISubprogram(name: "foo1", scope: null, spFlags: DISPFlagDefinition, unit: !1)
)",
                                                   Err, Context);
  ASSERT_TRUE(M1.get());

  StringLiteral M2Str = R"(
; ModuleID = '<string>'
source_filename = "<string2>"

@GV2 = external global i32

@GA2 = alias void (), ptr @Foo2

define void @Foo2() {
  ret void
}

!llvm.module.flags = !{!0}
!llvm.dbg.cu = !{!1}
!foo2 = !{!3}
!bar2 = !{!4}

!0 = !{i32 2, !"Debug Info Version", i32 3}
!1 = distinct !DICompileUnit(language: DW_LANG_C99, file: !2, producer: "clang2", isOptimized: true, flags: "-O2", runtimeVersion: 0, splitDebugFilename: "abc.debug", emissionKind: LineTablesOnly)
!2 = !DIFile(filename: "path/to/file2", directory: "/path/to/dir2")
!3 = !DILocation(line: 1234, column: 56, scope: !4)
!4 = distinct !DISubprogram(name: "foo2", scope: null, spFlags: DISPFlagDefinition, unit: !1)
)";
  {
    std::unique_ptr<Module> M2 = parseAssemblyString(M2Str, Err, Context);
    ASSERT_TRUE(M2.get());
    auto *GV1 = M1->getNamedValue("GV1");
    ASSERT_TRUE(GV1);
    auto *GV2 = M2->getNamedValue("GV2");
    ASSERT_TRUE(GV2);
    ASSERT_EQ(GV2->getParent(), &*M2);
    *M1 = std::move(*M2);
    ASSERT_EQ(GV2->getParent(), &*M1);
  }

  std::string M1Print;
  {
    llvm::raw_string_ostream Os(M1Print);
    Os << "\n" << *M1;
  }
  ASSERT_EQ(M2Str, M1Print);
}

} // end namespace