Fix test failures introduced by PR #113697 (#116941)
[llvm-project.git] / llvm / unittests / CodeGen / AMDGPUMetadataTest.cpp
blobeb86f5dd88989b7b96c62c7d4ea06c6b91021e39
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.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/IR/LegacyPassManager.h"
16 #include "llvm/IR/Module.h"
17 #include "llvm/MC/TargetRegistry.h"
18 #include "llvm/Pass.h"
19 #include "llvm/Support/TargetSelect.h"
20 #include "llvm/Target/TargetMachine.h"
21 #include "gtest/gtest.h"
23 namespace llvm {
25 namespace {
26 // Pass that adds global metadata
27 struct AddMetadataPass : public ModulePass {
28 std::string PalMDString;
30 public:
31 static char ID;
32 AddMetadataPass(std::string PalMDString)
33 : ModulePass(ID), PalMDString(PalMDString) {}
34 bool runOnModule(Module &M) override {
35 auto &Ctx = M.getContext();
36 auto *MD = M.getOrInsertNamedMetadata("amdgpu.pal.metadata.msgpack");
37 auto *PalMD = MDString::get(Ctx, PalMDString);
38 auto *TMD = MDTuple::get(Ctx, {PalMD});
39 MD->addOperand(TMD);
40 return true;
43 char AddMetadataPass::ID = 0;
44 } // end anonymous namespace
46 class AMDGPUSelectionDAGTest : public testing::Test {
47 protected:
48 static void SetUpTestCase() {
49 InitializeAllTargets();
50 InitializeAllTargetMCs();
53 void SetUp() override {
54 std::string Error;
55 const Target *T = TargetRegistry::lookupTarget("amdgcn--amdpal", Error);
56 if (!T)
57 GTEST_SKIP();
59 TargetOptions Options;
60 TM = std::unique_ptr<TargetMachine>(T->createTargetMachine(
61 "amdgcn--amdpal", "gfx1010", "", Options, std::nullopt));
62 if (!TM)
63 GTEST_SKIP();
65 LLVMContext Context;
66 std::unique_ptr<Module> M(new Module("TestModule", Context));
67 M->setDataLayout(TM->createDataLayout());
69 legacy::PassManager PM;
70 PM.add(new AddMetadataPass(PalMDString));
71 raw_svector_ostream OutStream(Elf);
72 if (TM->addPassesToEmitFile(PM, OutStream, nullptr,
73 CodeGenFileType::ObjectFile))
74 report_fatal_error("Target machine cannot emit a file of this type");
76 PM.run(*M);
79 static std::string PalMDString;
81 LLVMContext Context;
82 std::unique_ptr<TargetMachine> TM;
83 std::unique_ptr<Module> M;
84 SmallString<1024> Elf;
86 std::string AMDGPUSelectionDAGTest::PalMDString =
87 "\x81\xB0"
88 "amdpal.pipelines\x91\x81\xA4.api\xA6Vulkan";
90 TEST_F(AMDGPUSelectionDAGTest, checkMetadata) {
91 // Check that the string is contained in the ELF
92 EXPECT_NE(Elf.find("Vulkan"), std::string::npos);
95 } // end namespace llvm