[llvm] WebAssembly: Partially clean up subtarget construction (PR #224307)
via llvm-commits
llvm-commits at lists.llvm.org
Thu Sep 17 06:49:17 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-backend-webassembly
Author: Matt Arsenault (arsenm)
<details>
<summary>Changes</summary>
There should be just one getSubtargetImpl(Function&) override like every
other target, but this is relying on the global subtarget. Remove the
argumentless form since it's unnecessary.
WebAssemblyCoalesceFeaturesAndStripAtomics doesn't really need to construct
a full new subtarget for its purpose and can directly take the global subtarget
feature bits. This pass is also doing quite a lot of other bad things, like
mutating the TargetMachine.
Co-authored-by: Claude (Claude-Opus-4.8) <noreply@<!-- -->anthropic.com>
---
Full diff: https://github.com/llvm/llvm-project/pull/224307.diff
6 Files Affected:
- (modified) llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp (+3-3)
- (modified) llvm/lib/Target/WebAssembly/WebAssemblyCoalesceFeaturesAndStripAtomics.cpp (+12-15)
- (modified) llvm/lib/Target/WebAssembly/WebAssemblySubtarget.cpp (+2-3)
- (modified) llvm/lib/Target/WebAssembly/WebAssemblySubtarget.h (+2-2)
- (modified) llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp (+4-12)
- (modified) llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.h (+2-3)
``````````diff
diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
index 0d5d0ae04fa2d0..6bb03c614af191 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
@@ -224,7 +224,8 @@ void WebAssemblyAsmPrinter::emitGlobalVariable(const GlobalVariable *GV) {
// coalesces features before isel, so use the TargetMachine's
// module-wide subtarget to compute legal value types.
auto &WasmTM = static_cast<const WebAssemblyTargetMachine &>(TM);
- const WebAssemblySubtarget *ST = WasmTM.getSubtargetImpl();
+ const WebAssemblySubtarget *ST = WasmTM.getSubtargetImpl(
+ WasmTM.getTargetCPU(), WasmTM.getTargetFeatureString());
const WebAssemblyTargetLowering &TLI = *ST->getTargetLowering();
computeLegalValueVTs(TLI, GV->getParent()->getContext(),
GV->getDataLayout(), GlobalVT, VTs);
@@ -611,8 +612,7 @@ void WebAssemblyAsmPrinter::EmitTargetFeatures(Module &M) {
// If we never compiled a single function, Subtarget is null.
if (!Subtarget) {
Subtarget = static_cast<WebAssemblyTargetMachine &>(TM).getSubtargetImpl(
- std::string(TM.getTargetCPU()),
- std::string(TM.getTargetFeatureString()));
+ TM.getTargetCPU(), TM.getTargetFeatureString());
}
for (const SubtargetFeatureKV &KV : Subtarget->getAllProcessorFeatures()) {
EmitFeature(KV.key());
diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyCoalesceFeaturesAndStripAtomics.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyCoalesceFeaturesAndStripAtomics.cpp
index 8c4b24806c9a9b..08479e0f27617f 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyCoalesceFeaturesAndStripAtomics.cpp
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyCoalesceFeaturesAndStripAtomics.cpp
@@ -12,6 +12,7 @@
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/PassManager.h"
+#include "llvm/MC/MCSubtargetInfo.h"
#include "llvm/Pass.h"
#include "llvm/Transforms/Scalar/LowerAtomicPass.h"
@@ -48,10 +49,10 @@ ModulePass *llvm::createWebAssemblyCoalesceFeaturesAndStripAtomicsLegacyPass(
return new WebAssemblyCoalesceFeaturesAndStripAtomicsLegacy(&TM);
}
-static std::string getFeatureString(const WebAssemblySubtarget *ST,
+static std::string getFeatureString(const MCSubtargetInfo &STI,
const FeatureBitset &Features) {
std::string Ret;
- for (const SubtargetFeatureKV &KV : ST->getAllProcessorFeatures()) {
+ for (const SubtargetFeatureKV &KV : STI.getAllProcessorFeatures()) {
if (Features[KV.Value])
Ret += (StringRef("+") + KV.key() + ",").str();
else
@@ -69,26 +70,21 @@ coalesceFeatures(const Module &M, WebAssemblyTargetMachine *WasmTM) {
// disabled. If any function lacks a target-features attribute, it'll
// default to the target CPU from the `TargetMachine`.
FeatureBitset Features;
- // We need any MCSubtargetInfo to access WebAssemblyFeatureKV.
- const WebAssemblySubtarget *AnyST = nullptr;
+ bool AnyDefined = false;
for (auto &F : M) {
if (F.isDeclaration())
continue;
- AnyST = WasmTM->getSubtargetImpl(F);
- Features |= AnyST->getFeatureBits();
+ Features |= WasmTM->getSubtargetImpl(F)->getFeatureBits();
+ AnyDefined = true;
}
- // If we have no defined functions, use the target CPU from the
+ // If we have no defined functions, use the module-wide feature bits from the
// `TargetMachine`.
- if (!AnyST) {
- AnyST =
- WasmTM->getSubtargetImpl(std::string(WasmTM->getTargetCPU()),
- std::string(WasmTM->getTargetFeatureString()));
- Features = AnyST->getFeatureBits();
- }
+ if (!AnyDefined)
+ Features = WasmTM->getMCSubtargetInfo().getFeatureBits();
- return {Features, getFeatureString(AnyST, Features)};
+ return {Features, getFeatureString(WasmTM->getMCSubtargetInfo(), Features)};
}
static void replaceFeatures(Function &F, const std::string &Features) {
@@ -180,7 +176,8 @@ static bool coalesceFeaturesAndStripAtomics(Module &M,
// In cooperative threading mode, thread locals are meaningful even without
// atomics.
- const WebAssemblySubtarget *ST = WasmTM->getSubtargetImpl();
+ const WebAssemblySubtarget *ST = WasmTM->getSubtargetImpl(
+ WasmTM->getTargetCPU(), WasmTM->getTargetFeatureString());
bool CooperativeThreading = ST->hasCooperativeMultithreading();
if (!Features[WebAssembly::FeatureAtomics]) {
diff --git a/llvm/lib/Target/WebAssembly/WebAssemblySubtarget.cpp b/llvm/lib/Target/WebAssembly/WebAssemblySubtarget.cpp
index 0dcee7c660da48..1f964259fd4cad 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblySubtarget.cpp
+++ b/llvm/lib/Target/WebAssembly/WebAssemblySubtarget.cpp
@@ -50,9 +50,8 @@ WebAssemblySubtarget::initializeSubtargetDependencies(StringRef CPU,
return *this;
}
-WebAssemblySubtarget::WebAssemblySubtarget(const Triple &TT,
- const std::string &CPU,
- const std::string &FS,
+WebAssemblySubtarget::WebAssemblySubtarget(const Triple &TT, StringRef CPU,
+ StringRef FS,
const TargetMachine &TM)
: WebAssemblyGenSubtargetInfo(TT, CPU, /*TuneCPU*/ CPU, FS),
TargetTriple(TT), InstrInfo(initializeSubtargetDependencies(CPU, FS)),
diff --git a/llvm/lib/Target/WebAssembly/WebAssemblySubtarget.h b/llvm/lib/Target/WebAssembly/WebAssemblySubtarget.h
index fffe4d3a78f7ec..149c49dd344731 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblySubtarget.h
+++ b/llvm/lib/Target/WebAssembly/WebAssemblySubtarget.h
@@ -79,8 +79,8 @@ class WebAssemblySubtarget final : public WebAssemblyGenSubtargetInfo {
public:
/// This constructor initializes the data members to match that
/// of the specified triple.
- WebAssemblySubtarget(const Triple &TT, const std::string &CPU,
- const std::string &FS, const TargetMachine &TM);
+ WebAssemblySubtarget(const Triple &TT, StringRef CPU, StringRef FS,
+ const TargetMachine &TM);
const WebAssemblySelectionDAGInfo *getSelectionDAGInfo() const override {
return &TSInfo;
diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp
index 3b054f80027c53..066e8329e7f39d 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.cpp
@@ -220,15 +220,9 @@ WebAssemblyTargetMachine::WebAssemblyTargetMachine(
WebAssemblyTargetMachine::~WebAssemblyTargetMachine() = default; // anchor.
-const WebAssemblySubtarget *WebAssemblyTargetMachine::getSubtargetImpl() const {
- return getSubtargetImpl(std::string(getTargetCPU()),
- std::string(getTargetFeatureString()));
-}
-
const WebAssemblySubtarget *
-WebAssemblyTargetMachine::getSubtargetImpl(std::string CPU,
- std::string FS) const {
- auto &I = SubtargetMap[CPU + FS];
+WebAssemblyTargetMachine::getSubtargetImpl(StringRef CPU, StringRef FS) const {
+ auto &I = SubtargetMap[CPU.str() + FS.str()];
if (!I) {
I = std::make_unique<WebAssemblySubtarget>(TargetTriple, CPU, FS, *this);
}
@@ -240,10 +234,8 @@ WebAssemblyTargetMachine::getSubtargetImpl(const Function &F) const {
Attribute CPUAttr = F.getFnAttribute("target-cpu");
Attribute FSAttr = F.getFnAttribute("target-features");
- std::string CPU =
- CPUAttr.isValid() ? CPUAttr.getValueAsString().str() : TargetCPU;
- std::string FS =
- FSAttr.isValid() ? FSAttr.getValueAsString().str() : TargetFS;
+ StringRef CPU = CPUAttr.isValid() ? CPUAttr.getValueAsString() : TargetCPU;
+ StringRef FS = FSAttr.isValid() ? FSAttr.getValueAsString() : TargetFS;
return getSubtargetImpl(CPU, FS);
}
diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.h b/llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.h
index cff4d345b8516b..f21dc4b81dda45 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.h
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyTargetMachine.h
@@ -44,9 +44,8 @@ class WebAssemblyTargetMachine final : public CodeGenTargetMachineImpl {
~WebAssemblyTargetMachine() override;
- const WebAssemblySubtarget *getSubtargetImpl() const;
- const WebAssemblySubtarget *getSubtargetImpl(std::string CPU,
- std::string FS) const;
+ const WebAssemblySubtarget *getSubtargetImpl(StringRef CPU,
+ StringRef FS) const;
const WebAssemblySubtarget *
getSubtargetImpl(const Function &F) const override;
``````````
</details>
https://github.com/llvm/llvm-project/pull/224307
More information about the llvm-commits
mailing list