[llvm] [SystemZ] Implement .machine (push|pop) directives (PR #137302)
Dominik Steenken via llvm-commits
llvm-commits at lists.llvm.org
Mon Apr 28 04:04:51 PDT 2025
https://github.com/dominik-steenken updated https://github.com/llvm/llvm-project/pull/137302
>From 0834e7aa8b50416ed9af31b59ed40e5b72bbb06a Mon Sep 17 00:00:00 2001
From: Dominik Steenken <dost at de.ibm.com>
Date: Thu, 24 Apr 2025 11:38:22 +0200
Subject: [PATCH] [SystemZ] Implement .machine (push|pop) directives
The `.machine push` and `.machine pop` directives were missing from
the SystemZ Backend Asm Parser. This commit adds them, and expands the
corresponding test to test proper operation.
---
.../SystemZ/AsmParser/SystemZAsmParser.cpp | 53 +++++++++++++++----
.../MCTargetDesc/SystemZTargetStreamer.h | 8 +--
.../MC/SystemZ/machine-directive-invalid.s | 10 ++--
llvm/test/MC/SystemZ/machine-directive.s | 10 ++--
4 files changed, 60 insertions(+), 21 deletions(-)
diff --git a/llvm/lib/Target/SystemZ/AsmParser/SystemZAsmParser.cpp b/llvm/lib/Target/SystemZ/AsmParser/SystemZAsmParser.cpp
index 6d9a7a73f72db..7f03feba1737e 100644
--- a/llvm/lib/Target/SystemZ/AsmParser/SystemZAsmParser.cpp
+++ b/llvm/lib/Target/SystemZ/AsmParser/SystemZAsmParser.cpp
@@ -33,6 +33,7 @@
#include "llvm/Support/Casting.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/SMLoc.h"
+#include "llvm/TargetParser/SubtargetFeature.h"
#include <algorithm>
#include <cassert>
#include <cstddef>
@@ -410,6 +411,18 @@ class SystemZAsmParser : public MCTargetAsmParser {
private:
MCAsmParser &Parser;
+
+ // A vector to contain the stack of FeatureBitsets created by `.machine push`.
+ // `.machine pop` pops the top of the stack and resets `CurrentFeatures` to
+ // the result.
+ SmallVector<FeatureBitset> MachineStack;
+ // Specifies the current FeatureBitset. It is initialized from the
+ // SubtargetInfo handed to the constructor of the AsmParser. During parsing,
+ // it always reflects the current FeatureBitset used to determine which
+ // machine features are available, i.e. either the default or the result
+ // of the most reecent `.machine` directive.
+ FeatureBitset CurrentFeatures;
+
enum RegisterGroup {
RegGR,
RegFP,
@@ -494,16 +507,16 @@ class SystemZAsmParser : public MCTargetAsmParser {
public:
SystemZAsmParser(const MCSubtargetInfo &sti, MCAsmParser &parser,
- const MCInstrInfo &MII,
- const MCTargetOptions &Options)
- : MCTargetAsmParser(Options, sti, MII), Parser(parser) {
+ const MCInstrInfo &MII, const MCTargetOptions &Options)
+ : MCTargetAsmParser(Options, sti, MII), Parser(parser) {
MCAsmParserExtension::Initialize(Parser);
// Alias the .word directive to .short.
parser.addAliasForDirective(".word", ".short");
// Initialize the set of available features.
- setAvailableFeatures(ComputeAvailableFeatures(getSTI().getFeatureBits()));
+ CurrentFeatures = sti.getFeatureBits();
+ setAvailableFeatures(ComputeAvailableFeatures(CurrentFeatures));
}
// Override MCTargetAsmParser.
@@ -1382,17 +1395,35 @@ bool SystemZAsmParser::parseDirectiveMachine(SMLoc L) {
Parser.getTok().isNot(AsmToken::String))
return TokError("unexpected token in '.machine' directive");
- StringRef CPU = Parser.getTok().getIdentifier();
+ StringRef Id = Parser.getTok().getIdentifier();
+
+ // Parse push and pop directives first
+ if (Id == "push") {
+ // Push the Current FeatureBitSet onto the stack.
+ MachineStack.push_back(CurrentFeatures);
+ getTargetStreamer().emitMachine("push");
+ } else if (Id == "pop") {
+ // If the Stack is not empty, pop the topmost FeatureBitset and use it.
+ if (MachineStack.empty())
+ return TokError("pop without corresponding push in '.machine' directive");
+ CurrentFeatures = MachineStack.back();
+ MachineStack.pop_back();
+ setAvailableFeatures(CurrentFeatures);
+ getTargetStreamer().emitMachine("pop");
+ } else {
+ // Try to interpret the Identifier as a CPU spec and derive the
+ // FeatureBitset from that.
+ MCSubtargetInfo &STI = copySTI();
+ STI.setDefaultFeatures(Id, /*TuneCPU*/ Id, "");
+ CurrentFeatures = STI.getFeatureBits();
+ setAvailableFeatures(ComputeAvailableFeatures(CurrentFeatures));
+ getTargetStreamer().emitMachine(Id);
+ }
+
Parser.Lex();
if (parseEOL())
return true;
- MCSubtargetInfo &STI = copySTI();
- STI.setDefaultFeatures(CPU, /*TuneCPU*/ CPU, "");
- setAvailableFeatures(ComputeAvailableFeatures(STI.getFeatureBits()));
-
- getTargetStreamer().emitMachine(CPU);
-
return false;
}
diff --git a/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZTargetStreamer.h b/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZTargetStreamer.h
index 15617101fd08a..7e3c5f00525f4 100644
--- a/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZTargetStreamer.h
+++ b/llvm/lib/Target/SystemZ/MCTargetDesc/SystemZTargetStreamer.h
@@ -54,7 +54,7 @@ class SystemZTargetStreamer : public MCTargetStreamer {
void emitConstantPools() override;
- virtual void emitMachine(StringRef CPU) {};
+ virtual void emitMachine(StringRef CPUOrCommand) {};
virtual void emitExtern(StringRef Str) {};
@@ -85,7 +85,7 @@ class SystemZTargetHLASMStreamer : public SystemZTargetStreamer {
class SystemZTargetELFStreamer : public SystemZTargetStreamer {
public:
SystemZTargetELFStreamer(MCStreamer &S) : SystemZTargetStreamer(S) {}
- void emitMachine(StringRef CPU) override {}
+ void emitMachine(StringRef CPUOrCommand) override {}
};
class SystemZTargetGNUStreamer : public SystemZTargetStreamer {
@@ -94,8 +94,8 @@ class SystemZTargetGNUStreamer : public SystemZTargetStreamer {
public:
SystemZTargetGNUStreamer(MCStreamer &S, formatted_raw_ostream &OS)
: SystemZTargetStreamer(S), OS(OS) {}
- void emitMachine(StringRef CPU) override {
- OS << "\t.machine " << CPU << "\n";
+ void emitMachine(StringRef CPUOrCommand) override {
+ OS << "\t.machine " << CPUOrCommand << "\n";
}
};
diff --git a/llvm/test/MC/SystemZ/machine-directive-invalid.s b/llvm/test/MC/SystemZ/machine-directive-invalid.s
index 8b3147ecc62d4..bd4889912a5df 100644
--- a/llvm/test/MC/SystemZ/machine-directive-invalid.s
+++ b/llvm/test/MC/SystemZ/machine-directive-invalid.s
@@ -1,10 +1,14 @@
+// NOTE: Assertions have been autogenerated by utils/update_mc_test_checks.py UTC_ARGS: --version 5
# RUN: not llvm-mc -triple=s390x %s 2>&1 | FileCheck %s
-# CHECK: [[#@LINE+1]]:9: error: unexpected token in '.machine' directive
.machine
+// CHECK: :[[@LINE-1]]:9: error: unexpected token in '.machine' directive
+
+.machine pop
+// CHECK: :[[@LINE-1]]:10: error: pop without corresponding push in '.machine' directive
-# CHECK: [[#@LINE+1]]:10: error: unexpected token in '.machine' directive
.machine 42
+// CHECK: :[[@LINE-1]]:10: error: unexpected token in '.machine' directive
-# CHECK: [[#@LINE+1]]:13: error: expected newline
.machine z13+
+// CHECK: :[[@LINE-1]]:13: error: expected newline
diff --git a/llvm/test/MC/SystemZ/machine-directive.s b/llvm/test/MC/SystemZ/machine-directive.s
index aa71c6aea5572..3c973ef1fade4 100644
--- a/llvm/test/MC/SystemZ/machine-directive.s
+++ b/llvm/test/MC/SystemZ/machine-directive.s
@@ -5,16 +5,20 @@
# CHECK: ^
# CHECK-NOT: error:
+# CHECK: .machine push
# CHECK: .machine z13
# CHECK: vgbm %v0, 0
# CHECK: .machine zEC12
-# CHECK: .machine z13
+# CHECK: .machine pop
# CHECK: vgbm %v0, 3
+# CHECK: .machine pop
+.machine push
.machine z13
+.machine push
vgbm %v0, 0
.machine zEC12
vgbm %v0, 1
-.machine z13
+.machine pop
vgbm %v0, 3
-
+.machine pop
More information about the llvm-commits
mailing list