Skip to content

[X86Backend][M68KBackend] Make Ctx in X86MCInstLower (M68KInstLower) the same as AsmPrinter.OutContext #133352

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
Apr 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
859ed81
Use GetExternalSymbolSymbol for MO_ExternalSymbol.
weiweichen Mar 28, 2025
5b74939
Merge branch 'main' of https://github.com/llvm/llvm-project into weiw…
weiweichen Mar 28, 2025
ec3e34e
Fix formatting.
weiweichen Mar 28, 2025
cf82df0
Make change more explicit.
weiweichen Mar 28, 2025
fe9668b
Fix English.
weiweichen Mar 28, 2025
a2433c9
Merge branch 'main' of https://github.com/llvm/llvm-project into weiw…
weiweichen Mar 28, 2025
e395289
Add unittest.
weiweichen Mar 29, 2025
e7dedc3
Merge branch 'main' of https://github.com/llvm/llvm-project into weiw…
weiweichen Mar 29, 2025
fc41f66
Fix M68k backend.
weiweichen Mar 29, 2025
bc9791b
Add more comments.
weiweichen Apr 3, 2025
e4321ce
Merge branch 'main' of https://github.com/llvm/llvm-project into weiw…
weiweichen Apr 3, 2025
bf64c40
Tidy up unittest according to review comment.
weiweichen Apr 4, 2025
44dd9e3
Try to use AsmPrinter.OutContext for Ctx in X86MCInstLower.
weiweichen Apr 4, 2025
14009cc
Merge branch 'main' of https://github.com/llvm/llvm-project into weiw…
weiweichen Apr 4, 2025
d504073
Apply change to M68K too.
weiweichen Apr 4, 2025
c3d31b2
Merge branch 'main' of https://github.com/llvm/llvm-project into weiw…
weiweichen Apr 4, 2025
b9dcf9a
Revert change in cmake file.
weiweichen Apr 4, 2025
1deea3b
Make unittest skip if X86 backend is not built.
weiweichen Apr 4, 2025
65f158f
Merge branch 'main' of https://github.com/llvm/llvm-project into weiw…
weiweichen Apr 4, 2025
44cc5a6
Remove commented out code.
weiweichen Apr 4, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion llvm/lib/Target/M68k/M68kMCInstLower.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ using namespace llvm;
#define DEBUG_TYPE "m68k-mc-inst-lower"

M68kMCInstLower::M68kMCInstLower(MachineFunction &MF, M68kAsmPrinter &AP)
: Ctx(MF.getContext()), MF(MF), TM(MF.getTarget()), MAI(*TM.getMCAsmInfo()),
: Ctx(AP.OutContext), MF(MF), TM(MF.getTarget()), MAI(*TM.getMCAsmInfo()),
AsmPrinter(AP) {}

MCSymbol *
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Target/X86/X86MCInstLower.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ void X86AsmPrinter::EmitAndCountInstruction(MCInst &Inst) {

X86MCInstLower::X86MCInstLower(const MachineFunction &mf,
X86AsmPrinter &asmprinter)
: Ctx(mf.getContext()), MF(mf), TM(mf.getTarget()), MAI(*TM.getMCAsmInfo()),
AsmPrinter(asmprinter) {}
: Ctx(asmprinter.OutContext), MF(mf), TM(mf.getTarget()),
MAI(*TM.getMCAsmInfo()), AsmPrinter(asmprinter) {}

MachineModuleInfoMachO &X86MCInstLower::getMachOMMI() const {
return AsmPrinter.MMI->getObjFileInfo<MachineModuleInfoMachO>();
Expand Down
1 change: 1 addition & 0 deletions llvm/unittests/CodeGen/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ add_llvm_unittest(CodeGenTests
TargetOptionsTest.cpp
TestAsmPrinter.cpp
MLRegAllocDevelopmentFeatures.cpp
X86MCInstLowerTest.cpp
)

add_subdirectory(GlobalISel)
Expand Down
174 changes: 174 additions & 0 deletions llvm/unittests/CodeGen/X86MCInstLowerTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
//===- llvm/unittest/CodeGen/X86MCInstLowerTest.cpp
//-------------------------===//
//
// 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 "TestAsmPrinter.h"
#include "llvm/AsmParser/Parser.h"
#include "llvm/CodeGen/AsmPrinter.h"
#include "llvm/CodeGen/MachineModuleInfo.h"
#include "llvm/CodeGen/TargetLowering.h"
#include "llvm/CodeGen/TargetPassConfig.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/IR/MDBuilder.h"
#include "llvm/IR/Module.h"
#include "llvm/MC/MCStreamer.h"
#include "llvm/MC/TargetRegistry.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Target/TargetLoweringObjectFile.h"
#include "llvm/Target/TargetMachine.h"
#include "gtest/gtest.h"

namespace llvm {

class X86MCInstLowerTest : public testing::Test {
protected:
static void SetUpTestCase() {
InitializeAllTargetMCs();
InitializeAllTargetInfos();
InitializeAllTargets();
InitializeAllAsmPrinters();
}

// Function to setup codegen pipeline and returns the AsmPrinter.
AsmPrinter *addPassesToEmitFile(llvm::legacy::PassManagerBase &PM,
llvm::raw_pwrite_stream &Out,
llvm::CodeGenFileType FileType,
llvm::MachineModuleInfoWrapperPass *MMIWP) {
TargetPassConfig *PassConfig = TM->createPassConfig(PM);

PassConfig->setDisableVerify(true);
PM.add(PassConfig);
PM.add(MMIWP);

if (PassConfig->addISelPasses())
return nullptr;

PassConfig->addMachinePasses();
PassConfig->setInitialized();

MC.reset(new MCContext(TM->getTargetTriple(), TM->getMCAsmInfo(),
TM->getMCRegisterInfo(), TM->getMCSubtargetInfo()));
MC->setObjectFileInfo(TM->getObjFileLowering());
TM->getObjFileLowering()->Initialize(*MC, *TM);
MC->setObjectFileInfo(TM->getObjFileLowering());

// Use a new MCContext for AsmPrinter for testing.
// AsmPrinter.OutContext will be different from
// MachineFunction's MCContext in MMIWP.
Expected<std::unique_ptr<MCStreamer>> MCStreamerOrErr =
TM->createMCStreamer(Out, nullptr, FileType, *MC);

if (auto Err = MCStreamerOrErr.takeError())
return nullptr;

AsmPrinter *Printer =
TM->getTarget().createAsmPrinter(*TM, std::move(*MCStreamerOrErr));

if (!Printer)
return nullptr;

PM.add(Printer);

return Printer;
}

void SetUp() override {
// Module to compile.
const char *FooStr = R""""(
@G = external global i32

define i32 @foo() {
%1 = load i32, i32* @G; load the global variable
%2 = call i32 @f()
%3 = mul i32 %1, %2
ret i32 %3
}

declare i32 @f() #0
)"""";
StringRef AssemblyF(FooStr);

// Get target triple for X86_64
Triple TargetTriple("x86_64--");
std::string Error;
const Target *T = TargetRegistry::lookupTarget("", TargetTriple, Error);
// Skip the test if target is not built.
if (!T)
GTEST_SKIP();

// Get TargetMachine.
// Use Reloc::Model::PIC_ and CodeModel::Model::Large
// to get GOT during codegen as MO_ExternalSymbol.
TargetOptions Options;
TM = std::unique_ptr<TargetMachine>(T->createTargetMachine(
TargetTriple, "", "", Options, Reloc::Model::PIC_,
CodeModel::Model::Large, CodeGenOptLevel::Default));
if (!TM)
GTEST_SKIP();

SMDiagnostic SMError;

// Parse the module.
M = parseAssemblyString(AssemblyF, SMError, Context);
if (!M)
report_fatal_error(SMError.getMessage());
M->setDataLayout(TM->createDataLayout());

// Get llvm::Function from M
Foo = M->getFunction("foo");
if (!Foo)
report_fatal_error("foo?");

// Prepare the MCContext for codegen M.
// MachineFunction for Foo will have this MCContext.
MCFoo.reset(new MCContext(TargetTriple, TM->getMCAsmInfo(),
TM->getMCRegisterInfo(),
TM->getMCSubtargetInfo()));
MCFoo->setObjectFileInfo(TM->getObjFileLowering());
TM->getObjFileLowering()->Initialize(*MCFoo, *TM);
MCFoo->setObjectFileInfo(TM->getObjFileLowering());
}

LLVMContext Context;
std::unique_ptr<TargetMachine> TM;
std::unique_ptr<Module> M;

std::unique_ptr<MCContext> MC;
std::unique_ptr<MCContext> MCFoo;

Function *Foo;
std::unique_ptr<MachineFunction> MFFoo;
};

TEST_F(X86MCInstLowerTest, moExternalSymbol_MCSYMBOL) {

MachineModuleInfoWrapperPass *MMIWP =
new MachineModuleInfoWrapperPass(TM.get(), &*MCFoo);

legacy::PassManager PassMgrF;
SmallString<1024> Buf;
llvm::raw_svector_ostream OS(Buf);
AsmPrinter *Printer =
addPassesToEmitFile(PassMgrF, OS, CodeGenFileType::AssemblyFile, MMIWP);
PassMgrF.run(*M);

// Check GOT MCSymbol is from Printer.OutContext.
MCSymbol *GOTPrinterPtr =
Printer->OutContext.lookupSymbol("_GLOBAL_OFFSET_TABLE_");

// Check GOT MCSymbol is NOT from MachineFunction's MCContext.
MCSymbol *GOTMFCtxPtr =
MMIWP->getMMI().getMachineFunction(*Foo)->getContext().lookupSymbol(
"_GLOBAL_OFFSET_TABLE_");

EXPECT_NE(GOTPrinterPtr, nullptr);
EXPECT_EQ(GOTMFCtxPtr, nullptr);
}

} // end namespace llvm