1//===- llvm/unittest/CodeGen/AMDGPUMetadataTest.cpp -----------------------===//
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/// \file
10/// Test that amdgpu metadata that is added in a pass is read by the asm emitter
11/// and stored in the ELF.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/IR/LegacyPassManager.h"
16#include "llvm/MC/TargetRegistry.h"
17#include "llvm/Pass.h"
18#include "llvm/Support/TargetSelect.h"
19#include "llvm/Target/TargetMachine.h"
20#include "gtest/gtest.h"
21
22namespace llvm {
23
24namespace {
25// Pass that adds global metadata
26struct AddMetadataPass : public ModulePass {
27 std::string PalMDString;
28
29public:
30 static char ID;
31 AddMetadataPass(std::string PalMDString)
32 : ModulePass(ID), PalMDString(PalMDString) {}
33 bool runOnModule(Module &M) override {
34 auto &Ctx = M.getContext();
35 auto *MD = M.getOrInsertNamedMetadata(Name: "amdgpu.pal.metadata.msgpack");
36 auto *PalMD = MDString::get(Context&: Ctx, Str: PalMDString);
37 auto *TMD = MDTuple::get(Context&: Ctx, MDs: {PalMD});
38 MD->addOperand(M: TMD);
39 return true;
40 }
41};
42char AddMetadataPass::ID = 0;
43} // end anonymous namespace
44
45class AMDGPUSelectionDAGTest : public testing::Test {
46protected:
47 static void SetUpTestCase() {
48 InitializeAllTargets();
49 InitializeAllTargetMCs();
50 }
51
52 void SetUp() override {
53 std::string Error;
54 const Target *T = TargetRegistry::lookupTarget(Triple: "amdgcn--amdpal", Error);
55 if (!T)
56 GTEST_SKIP();
57
58 TargetOptions Options;
59 TM = std::unique_ptr<LLVMTargetMachine>(
60 static_cast<LLVMTargetMachine *>(T->createTargetMachine(
61 TT: "amdgcn--amdpal", CPU: "gfx1010", Features: "", Options, RM: std::nullopt)));
62 if (!TM)
63 GTEST_SKIP();
64
65 LLVMContext Context;
66 std::unique_ptr<Module> M(new Module("TestModule", Context));
67 M->setDataLayout(TM->createDataLayout());
68
69 legacy::PassManager PM;
70 PM.add(P: new AddMetadataPass(PalMDString));
71 raw_svector_ostream OutStream(Elf);
72 if (TM->addPassesToEmitFile(PM, Out&: OutStream, DwoOut: nullptr,
73 FileType: CodeGenFileType::ObjectFile))
74 report_fatal_error(reason: "Target machine cannot emit a file of this type");
75
76 PM.run(M&: *M);
77 }
78
79 static std::string PalMDString;
80
81 LLVMContext Context;
82 std::unique_ptr<LLVMTargetMachine> TM;
83 std::unique_ptr<Module> M;
84 SmallString<1024> Elf;
85};
86std::string AMDGPUSelectionDAGTest::PalMDString =
87 "\x81\xB0"
88 "amdpal.pipelines\x91\x81\xA4.api\xA6Vulkan";
89
90TEST_F(AMDGPUSelectionDAGTest, checkMetadata) {
91 // Check that the string is contained in the ELF
92 EXPECT_NE(Elf.find("Vulkan"), std::string::npos);
93}
94
95} // end namespace llvm
96

source code of llvm/unittests/CodeGen/AMDGPUMetadataTest.cpp