Skip to content

Commit 4776451

Browse files
[X86][NewPM] Port lower-amx-intrinsics to NewPM
Reviewers: paperchalice, phoebewang, arsenm Reviewed By: arsenm Pull Request: #165113
1 parent 0ae0ac0 commit 4776451

File tree

6 files changed

+56
-15
lines changed

6 files changed

+56
-15
lines changed

llvm/lib/Target/X86/X86.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,18 @@ FunctionPass *createX86LowerAMXTypeLegacyPass();
179179

180180
/// The pass transforms amx intrinsics to scalar operation if the function has
181181
/// optnone attribute or it is O0.
182-
FunctionPass *createX86LowerAMXIntrinsicsPass();
182+
class X86LowerAMXIntrinsicsPass
183+
: public PassInfoMixin<X86LowerAMXIntrinsicsPass> {
184+
private:
185+
const TargetMachine *TM;
186+
187+
public:
188+
X86LowerAMXIntrinsicsPass(const TargetMachine *TM) : TM(TM) {}
189+
PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM);
190+
static bool isRequired() { return true; }
191+
};
192+
193+
FunctionPass *createX86LowerAMXIntrinsicsLegacyPass();
183194

184195
InstructionSelector *createX86InstructionSelector(const X86TargetMachine &TM,
185196
const X86Subtarget &,

llvm/lib/Target/X86/X86LowerAMXIntrinsics.cpp

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,15 @@
2323
#include "llvm/CodeGen/Passes.h"
2424
#include "llvm/CodeGen/TargetPassConfig.h"
2525
#include "llvm/CodeGen/ValueTypes.h"
26+
#include "llvm/IR/Analysis.h"
2627
#include "llvm/IR/DataLayout.h"
28+
#include "llvm/IR/Dominators.h"
2729
#include "llvm/IR/Function.h"
2830
#include "llvm/IR/IRBuilder.h"
2931
#include "llvm/IR/Instructions.h"
3032
#include "llvm/IR/IntrinsicInst.h"
3133
#include "llvm/IR/IntrinsicsX86.h"
34+
#include "llvm/IR/PassManager.h"
3235
#include "llvm/IR/PatternMatch.h"
3336
#include "llvm/InitializePasses.h"
3437
#include "llvm/Pass.h"
@@ -40,7 +43,7 @@
4043
using namespace llvm;
4144
using namespace PatternMatch;
4245

43-
#define DEBUG_TYPE "lower-amx-intrinsics"
46+
#define DEBUG_TYPE "x86-lower-amx-intrinsics"
4447

4548
#ifndef NDEBUG
4649
static bool isV256I32Ty(Type *Ty) {
@@ -626,6 +629,37 @@ bool X86LowerAMXIntrinsics::visit() {
626629
return C;
627630
}
628631

632+
namespace {
633+
bool shouldRunLowerAMXIntrinsics(const Function &F, const TargetMachine *TM) {
634+
return X86ScalarizeAMX && (F.hasFnAttribute(Attribute::OptimizeNone) ||
635+
TM->getOptLevel() == CodeGenOptLevel::None);
636+
}
637+
638+
bool runLowerAMXIntrinsics(Function &F, DominatorTree *DT, LoopInfo *LI) {
639+
DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Lazy);
640+
641+
X86LowerAMXIntrinsics LAT(F, DTU, LI);
642+
return LAT.visit();
643+
}
644+
} // namespace
645+
646+
PreservedAnalyses X86LowerAMXIntrinsicsPass::run(Function &F,
647+
FunctionAnalysisManager &FAM) {
648+
if (!shouldRunLowerAMXIntrinsics(F, TM))
649+
return PreservedAnalyses::all();
650+
651+
DominatorTree &DT = FAM.getResult<DominatorTreeAnalysis>(F);
652+
LoopInfo &LI = FAM.getResult<LoopAnalysis>(F);
653+
bool Changed = runLowerAMXIntrinsics(F, &DT, &LI);
654+
if (!Changed)
655+
return PreservedAnalyses::all();
656+
657+
PreservedAnalyses PA = PreservedAnalyses::none();
658+
PA.preserve<DominatorTreeAnalysis>();
659+
PA.preserve<LoopAnalysis>();
660+
return PA;
661+
}
662+
629663
namespace {
630664
class X86LowerAMXIntrinsicsLegacyPass : public FunctionPass {
631665
public:
@@ -634,21 +668,15 @@ class X86LowerAMXIntrinsicsLegacyPass : public FunctionPass {
634668
X86LowerAMXIntrinsicsLegacyPass() : FunctionPass(ID) {}
635669

636670
bool runOnFunction(Function &F) override {
637-
if (!X86ScalarizeAMX)
638-
return false;
639671
TargetMachine *TM = &getAnalysis<TargetPassConfig>().getTM<TargetMachine>();
640-
if (!F.hasFnAttribute(Attribute::OptimizeNone) &&
641-
TM->getOptLevel() != CodeGenOptLevel::None)
672+
if (!shouldRunLowerAMXIntrinsics(F, TM))
642673
return false;
643674

644675
auto *DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>();
645676
auto *DT = DTWP ? &DTWP->getDomTree() : nullptr;
646677
auto *LIWP = getAnalysisIfAvailable<LoopInfoWrapperPass>();
647678
auto *LI = LIWP ? &LIWP->getLoopInfo() : nullptr;
648-
DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Lazy);
649-
650-
X86LowerAMXIntrinsics LAT(F, DTU, LI);
651-
return LAT.visit();
679+
return runLowerAMXIntrinsics(F, DT, LI);
652680
}
653681
StringRef getPassName() const override { return "Lower AMX intrinsics"; }
654682

@@ -668,6 +696,6 @@ INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)
668696
INITIALIZE_PASS_END(X86LowerAMXIntrinsicsLegacyPass, DEBUG_TYPE, PassName,
669697
false, false)
670698

671-
FunctionPass *llvm::createX86LowerAMXIntrinsicsPass() {
699+
FunctionPass *llvm::createX86LowerAMXIntrinsicsLegacyPass() {
672700
return new X86LowerAMXIntrinsicsLegacyPass();
673701
}

llvm/lib/Target/X86/X86PassRegistry.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515
#ifndef FUNCTION_PASS
1616
#define FUNCTION_PASS(NAME, CREATE_PASS)
1717
#endif
18+
FUNCTION_PASS("x86-lower-amx-intrinsics", X86LowerAMXIntrinsicsPass(this))
1819
FUNCTION_PASS("x86-lower-amx-type", X86LowerAMXTypePass(this))
1920
#undef FUNCTION_PASS
2021

2122
#ifndef DUMMY_FUNCTION_PASS
2223
#define DUMMY_FUNCTION_PASS(NAME, CREATE_PASS)
2324
#endif
24-
DUMMY_FUNCTION_PASS("lower-amx-intrinsics", X86LowerAMXIntrinsics(*this))
2525
DUMMY_FUNCTION_PASS("x86-partial-reduction", X86PartialReduction())
2626
DUMMY_FUNCTION_PASS("x86-winehstate", WinEHStatePass())
2727
#undef DUMMY_FUNCTION_PASS

llvm/lib/Target/X86/X86TargetMachine.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ void X86PassConfig::addIRPasses() {
422422

423423
// We add both pass anyway and when these two passes run, we skip the pass
424424
// based on the option level and option attribute.
425-
addPass(createX86LowerAMXIntrinsicsPass());
425+
addPass(createX86LowerAMXIntrinsicsLegacyPass());
426426
addPass(createX86LowerAMXTypeLegacyPass());
427427

428428
TargetPassConfig::addIRPasses();

llvm/test/CodeGen/X86/AMX/amx-low-intrinsics-no-amx-bitcast.ll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2-
; RUN: opt -mtriple=x86_64 -lower-amx-intrinsics -enable-x86-scalar-amx=true %s -S | FileCheck %s
2+
; RUN: opt -mtriple=x86_64 -x86-lower-amx-intrinsics -enable-x86-scalar-amx=true %s -S | FileCheck %s
3+
; RUN: opt -mtriple=x86_64 -passes=x86-lower-amx-intrinsics -enable-x86-scalar-amx=true %s -S | FileCheck %s
34

45
define dso_local void @test_no_bitcast(ptr %A_mem, ptr %B_mem, ptr %C_mem) local_unnamed_addr #0 {
56
; CHECK-LABEL: @test_no_bitcast(

llvm/test/CodeGen/X86/AMX/amx-low-intrinsics.ll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2-
; RUN: opt -mtriple=x86_64 -lower-amx-intrinsics -enable-x86-scalar-amx=true %s -S | FileCheck %s
2+
; RUN: opt -mtriple=x86_64 -x86-lower-amx-intrinsics -enable-x86-scalar-amx=true %s -S | FileCheck %s
3+
; RUN: opt -mtriple=x86_64 -passes=x86-lower-amx-intrinsics -enable-x86-scalar-amx=true %s -S | FileCheck %s
34

45
define dso_local void @test_amx_load_non_O0(i16 signext %row, i16 signext %col, ptr%ptr, i64 %stride, ptr %vptr) {
56
; CHECK-LABEL: @test_amx_load_non_O0(

0 commit comments

Comments
 (0)