From b9c78deba3eda6137b2a0d2373ffe52279918679 Mon Sep 17 00:00:00 2001 From: Krzysztof Parzyszek Date: Tue, 23 May 2023 13:08:34 -0700 Subject: [PATCH] [Hexagon] Add more debugging options and dumps to HVC --- llvm/lib/Target/Hexagon/HexagonVectorCombine.cpp | 67 +++++++++++++++++++--- .../CodeGen/Hexagon/autohvx/vector-align-store.ll | 2 +- .../CodeGen/Hexagon/autohvx/vector-align-tbaa.ll | 2 +- 3 files changed, 60 insertions(+), 11 deletions(-) diff --git a/llvm/lib/Target/Hexagon/HexagonVectorCombine.cpp b/llvm/lib/Target/Hexagon/HexagonVectorCombine.cpp index 7e46516..294674c 100644 --- a/llvm/lib/Target/Hexagon/HexagonVectorCombine.cpp +++ b/llvm/lib/Target/Hexagon/HexagonVectorCombine.cpp @@ -58,7 +58,15 @@ using namespace llvm; namespace { -cl::opt AlignFullHvxStores("hvc-align-full-hvx-stores", cl::Hidden); +cl::opt DumpModule("hvc-dump-module", cl::Hidden); +cl::opt VAEnabled("hvc-va", cl::Hidden, cl::init(true)); // Align +cl::opt VIEnabled("hvc-vi", cl::Hidden, cl::init(true)); // Idioms +cl::opt VADoFullStores("hvc-va-full-stores", cl::Hidden); + +cl::opt VAGroupCountLimit("hvc-va-group-count-limit", cl::Hidden, + cl::init(~0)); +cl::opt VAGroupSizeLimit("hvc-va-group-size-limit", cl::Hidden, + cl::init(~0)); class HexagonVectorCombine { public: @@ -223,6 +231,7 @@ private: struct MoveGroup { MoveGroup(const AddrInfo &AI, Instruction *B, bool Hvx, bool Load) : Base(B), Main{AI.Inst}, Clones{}, IsHvx(Hvx), IsLoad(Load) {} + MoveGroup() = default; Instruction *Base; // Base instruction of the parent address group. InstList Main; // Main group of instructions. InstList Deps; // List of dependencies. @@ -374,6 +383,12 @@ raw_ostream &operator<<(raw_ostream &OS, const AlignVectors::MoveGroup &MG) { OS << "Deps\n"; for (Instruction *I : MG.Deps) OS << " " << *I << '\n'; + OS << "Clones\n"; + for (auto [K, V] : MG.Clones) { + OS << " "; + K->printAsOperand(OS, false); + OS << "\t-> " << *V << '\n'; + } return OS; } @@ -929,9 +944,14 @@ auto AlignVectors::createLoadGroups(const AddrList &Group) const -> MoveList { // Form load groups. // To avoid complications with moving code across basic blocks, only form // groups that are contained within a single basic block. + unsigned SizeLimit = VAGroupSizeLimit; + if (SizeLimit == 0) + return {}; auto tryAddTo = [&](const AddrInfo &Info, MoveGroup &Move) { assert(!Move.Main.empty() && "Move group should have non-empty Main"); + if (Move.Main.size() >= SizeLimit) + return false; // Don't mix HVX and non-HVX instructions. if (Move.IsHvx != isHvx(Info)) return false; @@ -979,9 +999,14 @@ auto AlignVectors::createStoreGroups(const AddrList &Group) const -> MoveList { // Form store groups. // To avoid complications with moving code across basic blocks, only form // groups that are contained within a single basic block. + unsigned SizeLimit = VAGroupSizeLimit; + if (SizeLimit == 0) + return {}; auto tryAddTo = [&](const AddrInfo &Info, MoveGroup &Move) { assert(!Move.Main.empty() && "Move group should have non-empty Main"); + if (Move.Main.size() >= SizeLimit) + return false; // For stores with return values we'd have to collect downward depenencies. // There are no such stores that we handle at the moment, so omit that. assert(Info.Inst->getType()->isVoidTy() && @@ -1021,7 +1046,7 @@ auto AlignVectors::createStoreGroups(const AddrList &Group) const -> MoveList { // Erase groups where every store is a full HVX vector. The reason is that // aligning predicated stores generates complex code that may be less // efficient than a sequence of unaligned vector stores. - if (!AlignFullHvxStores) { + if (!VADoFullStores) { erase_if(StoreGroups, [this](const MoveGroup &G) { return G.IsHvx && llvm::all_of(G.Main, [this](Instruction *S) { auto MaybeInfo = this->getAddrInfo(*S); @@ -1511,8 +1536,8 @@ auto AlignVectors::realignGroup(const MoveGroup &Move) const -> bool { auto AlignVectors::makeTestIfUnaligned(IRBuilderBase &Builder, Value *AlignVal, int Alignment) const -> Value * { auto *AlignTy = AlignVal->getType(); - Value *And = - Builder.CreateAnd(AlignVal, ConstantInt::get(AlignTy, Alignment - 1)); + Value *And = Builder.CreateAnd( + AlignVal, ConstantInt::get(AlignTy, Alignment - 1), "and"); Value *Zero = ConstantInt::get(AlignTy, 0); return Builder.CreateICmpNE(And, Zero, "isz"); } @@ -1527,7 +1552,8 @@ auto AlignVectors::isSectorTy(Type *Ty) const -> bool { } auto AlignVectors::run() -> bool { - LLVM_DEBUG(dbgs() << "Running HVC::AlignVectors\n"); + LLVM_DEBUG(dbgs() << "Running HVC::AlignVectors on " << HVC.F.getName() + << '\n'); if (!createAddressGroups()) return false; @@ -1556,6 +1582,20 @@ auto AlignVectors::run() -> bool { dbgs() << G << "\n"; }); + // Cumulative limit on the number of groups. + unsigned CountLimit = VAGroupCountLimit; + if (CountLimit == 0) + return false; + + if (LoadGroups.size() > CountLimit) { + LoadGroups.resize(CountLimit); + StoreGroups.clear(); + } else { + unsigned StoreLimit = CountLimit - LoadGroups.size(); + if (StoreGroups.size() > StoreLimit) + StoreGroups.resize(StoreLimit); + } + for (auto &M : LoadGroups) Changed |= moveTogether(M); for (auto &M : StoreGroups) @@ -2115,13 +2155,22 @@ auto HvxIdioms::run() -> bool { // --- End HvxIdioms auto HexagonVectorCombine::run() -> bool { - if (!HST.useHVXOps()) - return false; + if (DumpModule) + dbgs() << "Module before HexagonVectorCombine\n" << *F.getParent(); bool Changed = false; - Changed |= AlignVectors(*this).run(); - Changed |= HvxIdioms(*this).run(); + if (HST.useHVXOps()) { + if (VAEnabled) + Changed |= AlignVectors(*this).run(); + if (VIEnabled) + Changed |= HvxIdioms(*this).run(); + } + if (DumpModule) { + dbgs() << "Module " << (Changed ? "(modified)" : "(unchanged)") + << " after HexagonVectorCombine\n" + << *F.getParent(); + } return Changed; } diff --git a/llvm/test/CodeGen/Hexagon/autohvx/vector-align-store.ll b/llvm/test/CodeGen/Hexagon/autohvx/vector-align-store.ll index 9d10551..876b02b 100644 --- a/llvm/test/CodeGen/Hexagon/autohvx/vector-align-store.ll +++ b/llvm/test/CodeGen/Hexagon/autohvx/vector-align-store.ll @@ -1,4 +1,4 @@ -; RUN: llc -march=hexagon -hvc-align-full-hvx-stores < %s | FileCheck %s +; RUN: llc -march=hexagon -hvc-va-full-stores < %s | FileCheck %s ; Make sure we generate 3 aligned stores. ; CHECK: vmem({{.*}}) = diff --git a/llvm/test/CodeGen/Hexagon/autohvx/vector-align-tbaa.ll b/llvm/test/CodeGen/Hexagon/autohvx/vector-align-tbaa.ll index a931d14..333ecc1 100644 --- a/llvm/test/CodeGen/Hexagon/autohvx/vector-align-tbaa.ll +++ b/llvm/test/CodeGen/Hexagon/autohvx/vector-align-tbaa.ll @@ -1,5 +1,5 @@ ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py -; RUN: opt -mtriple=hexagon -S -hexagon-vc -instcombine -hvc-align-full-hvx-stores < %s | FileCheck %s +; RUN: opt -mtriple=hexagon -S -hexagon-vc -instcombine -hvc-va-full-stores < %s | FileCheck %s ; Check that Hexagon Vector Combine propagates (TBAA) metadata to the ; generated output. (Use instcombine to clean the output up a bit.) -- 2.7.4