[llvm] [BOLT] [PowerPC] Port (PR #140894)

via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 26 10:41:17 PDT 2025


https://github.com/kostasalv updated https://github.com/llvm/llvm-project/pull/140894

>From 47e7f7a9d409caa6b72bc203992529bf6ccf71d9 Mon Sep 17 00:00:00 2001
From: Kostas Alvertis <konstantinos.alvertis at gmail.com>
Date: Tue, 20 May 2025 16:24:48 +0100
Subject: [PATCH 01/19] Work in Progress: BOLT PPC support

---
 bolt/lib/Target/PowerPC/PPCMCPlusBuilder.cpp | 11 +++++++++++
 bolt/lib/Target/PowerPC/PPCMCSymbolizer.cpp  |  0
 bolt/lib/Target/PowerPC/PPCMCSymbolizer.h    |  0
 3 files changed, 11 insertions(+)
 create mode 100644 bolt/lib/Target/PowerPC/PPCMCPlusBuilder.cpp
 create mode 100644 bolt/lib/Target/PowerPC/PPCMCSymbolizer.cpp
 create mode 100644 bolt/lib/Target/PowerPC/PPCMCSymbolizer.h

diff --git a/bolt/lib/Target/PowerPC/PPCMCPlusBuilder.cpp b/bolt/lib/Target/PowerPC/PPCMCPlusBuilder.cpp
new file mode 100644
index 0000000000000..6402e84ae2097
--- /dev/null
+++ b/bolt/lib/Target/PowerPC/PPCMCPlusBuilder.cpp
@@ -0,0 +1,11 @@
+//===- bolt/Target/PowerPC/PPCMCPlusBuilder.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
+//
+//===----------------------------------------------------------------------===//
+//
+// This file provides PowerPC-specific MCPlus builder.
+//
+//===----------------------------------------------------------------------===//
diff --git a/bolt/lib/Target/PowerPC/PPCMCSymbolizer.cpp b/bolt/lib/Target/PowerPC/PPCMCSymbolizer.cpp
new file mode 100644
index 0000000000000..e69de29bb2d1d
diff --git a/bolt/lib/Target/PowerPC/PPCMCSymbolizer.h b/bolt/lib/Target/PowerPC/PPCMCSymbolizer.h
new file mode 100644
index 0000000000000..e69de29bb2d1d

>From 5520b2c480b888fbbe1658ea3ac95e07b26e578b Mon Sep 17 00:00:00 2001
From: Kostas Alvertis <konstantinos.alvertis at gmail.com>
Date: Tue, 20 May 2025 17:12:43 +0100
Subject: [PATCH 02/19] [PPC][BOLT] Port createPushRegisters for PowerPC

---
 bolt/lib/Target/PowerPC/PPCMCPlusBuilder.cpp | 33 ++++++++++++++++++++
 bolt/lib/Target/PowerPC/PPCMCSymbolizer.cpp  |  7 +++++
 bolt/lib/Target/PowerPC/PPCMCSymbolizer.h    |  7 +++++
 3 files changed, 47 insertions(+)

diff --git a/bolt/lib/Target/PowerPC/PPCMCPlusBuilder.cpp b/bolt/lib/Target/PowerPC/PPCMCPlusBuilder.cpp
index 6402e84ae2097..7757ea141313b 100644
--- a/bolt/lib/Target/PowerPC/PPCMCPlusBuilder.cpp
+++ b/bolt/lib/Target/PowerPC/PPCMCPlusBuilder.cpp
@@ -9,3 +9,36 @@
 // This file provides PowerPC-specific MCPlus builder.
 //
 //===----------------------------------------------------------------------===//
+
+#include "bolt/Core/MCPlusBuilder.h"
+#include "llvm/MC/MCInst.h"
+#include "llvm/MC/MCPhysReg.h"
+#include "llvm/Target/PowerPC/PPCInstrInfo.h"
+#include "llvm/Target/PowerPC/PPCRegisterInfo.h"
+
+namespace llvm {
+namespace bolt {
+
+class PPCMCPlusBuilder : public MCPlusBuilder{
+public:
+    using MCPlusBuilder::MCPlusBuilder;
+
+    // Create instructions to push two registers onto the stack
+    static void createPushRegisters(MCInst &Inst1, MCInst &Inst2, MCPhysReg Reg1, MCPhysReg /*Reg2*/){
+
+        Inst1.clear();
+        Inst1.setOpcode(PPC::STDU);
+        Inst1.addOperand(MCOperand::createReg(PPC::R1)); // destination (SP)
+        Inst1.addOperand(MCOperand::createReg(PPC::R1)); // base (SP)
+        Inst1.addOperand(MCOperand::createImm(-16));     // offset
+
+        Inst2.clear();
+        Inst2.setOpcode(PPC::STD);
+        Inst2.addOperand(MCOperand::createReg(Reg1));     // source register
+        Inst2.addOperand(MCOperand::createReg(PPC::R1));  // base (SP)
+        Inst2.addOperand(MCOperand::createImm(0));        // offset
+    }
+};
+
+} // namespace bolt
+} // namespace llvm
\ No newline at end of file
diff --git a/bolt/lib/Target/PowerPC/PPCMCSymbolizer.cpp b/bolt/lib/Target/PowerPC/PPCMCSymbolizer.cpp
index e69de29bb2d1d..c5edc77e191f3 100644
--- a/bolt/lib/Target/PowerPC/PPCMCSymbolizer.cpp
+++ b/bolt/lib/Target/PowerPC/PPCMCSymbolizer.cpp
@@ -0,0 +1,7 @@
+//===- bolt/Target/PowerPC/PPCMCSymbolizer.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
+//
+//===----------------------------------------------------------------------===//
\ No newline at end of file
diff --git a/bolt/lib/Target/PowerPC/PPCMCSymbolizer.h b/bolt/lib/Target/PowerPC/PPCMCSymbolizer.h
index e69de29bb2d1d..b4bfe7e1593c9 100644
--- a/bolt/lib/Target/PowerPC/PPCMCSymbolizer.h
+++ b/bolt/lib/Target/PowerPC/PPCMCSymbolizer.h
@@ -0,0 +1,7 @@
+//===- bolt/Target/PowerPC/PPCMCSymbolizer.cpp --------------*- C++ -*-===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
\ No newline at end of file

>From 3e29e6fe14a6790bd84e3412c0fba6e3b7d8ce85 Mon Sep 17 00:00:00 2001
From: Kostas Alvertis <konstantinos.alvertis at gmail.com>
Date: Tue, 20 May 2025 18:42:52 +0100
Subject: [PATCH 03/19] [PPC][BOLT] Add CMakeLists

---
 bolt/lib/Target/PowerPC/CMakeLists.txt | 3 +++
 1 file changed, 3 insertions(+)
 create mode 100644 bolt/lib/Target/PowerPC/CMakeLists.txt

diff --git a/bolt/lib/Target/PowerPC/CMakeLists.txt b/bolt/lib/Target/PowerPC/CMakeLists.txt
new file mode 100644
index 0000000000000..cafb951702cae
--- /dev/null
+++ b/bolt/lib/Target/PowerPC/CMakeLists.txt
@@ -0,0 +1,3 @@
+add_llvm_library(BoltPPC
+    PPCMCPlusBuilder.cpp
+  )
\ No newline at end of file

>From d7c885d6073ea2033678d29b62ece21f2fab97ca Mon Sep 17 00:00:00 2001
From: Kostas Alvertis <konstantinos.alvertis at gmail.com>
Date: Tue, 20 May 2025 18:47:08 +0100
Subject: [PATCH 04/19] [PPC][BOLT] Adding PowerPC in parent CMakeLists

---
 bolt/CMakeLists.txt | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/bolt/CMakeLists.txt b/bolt/CMakeLists.txt
index 5c7d51e1e398c..0ac3286ecef65 100644
--- a/bolt/CMakeLists.txt
+++ b/bolt/CMakeLists.txt
@@ -62,7 +62,7 @@ endif() # standalone
 
 # Determine default set of targets to build -- the intersection of
 # those BOLT supports and those LLVM is targeting.
-set(BOLT_TARGETS_TO_BUILD_all "AArch64;X86;RISCV")
+set(BOLT_TARGETS_TO_BUILD_all "AArch64;X86;RISCV;PowerPC")
 set(BOLT_TARGETS_TO_BUILD_default)
 foreach (tgt ${BOLT_TARGETS_TO_BUILD_all})
   if (tgt IN_LIST LLVM_TARGETS_TO_BUILD)

>From 9640b636eab7543472cdfa9cb27f91b81da84a69 Mon Sep 17 00:00:00 2001
From: Kostas Alvertis <konstantinos.alvertis at gmail.com>
Date: Tue, 20 May 2025 19:21:48 +0100
Subject: [PATCH 05/19] [PPC][BOLT] Adding factory function

---
 bolt/lib/Target/PowerPC/PPCMCPlusBuilder.cpp | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/bolt/lib/Target/PowerPC/PPCMCPlusBuilder.cpp b/bolt/lib/Target/PowerPC/PPCMCPlusBuilder.cpp
index 7757ea141313b..a43ddd4bfc413 100644
--- a/bolt/lib/Target/PowerPC/PPCMCPlusBuilder.cpp
+++ b/bolt/lib/Target/PowerPC/PPCMCPlusBuilder.cpp
@@ -40,5 +40,13 @@ class PPCMCPlusBuilder : public MCPlusBuilder{
     }
 };
 
+MCPlusBuilder *createPowerPCMCPlusBuilder(const MCInstrAnalysis *Analysis,
+                                          const MCInstrInfo *Info,
+                                          const MCRegisterInfo *RegInfo,
+                                          const MCSubtargetInfo *STI) {
+  return new PPCMCPlusBuilder(Analysis, Info, RegInfo, STI);
+}
+
 } // namespace bolt
-} // namespace llvm
\ No newline at end of file
+} // namespace llvm
+

>From 0f61f98fe26f43280cf197ffc67a2e45343ec00a Mon Sep 17 00:00:00 2001
From: Kostas Alvertis <konstantinos.alvertis at gmail.com>
Date: Tue, 20 May 2025 19:50:15 +0100
Subject: [PATCH 06/19] [PPC][BOLT] Adding PCC factory function declaration

---
 bolt/include/bolt/Core/MCPlusBuilder.h | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/bolt/include/bolt/Core/MCPlusBuilder.h b/bolt/include/bolt/Core/MCPlusBuilder.h
index ae04891e791f9..32ccc21a4be59 100644
--- a/bolt/include/bolt/Core/MCPlusBuilder.h
+++ b/bolt/include/bolt/Core/MCPlusBuilder.h
@@ -2346,6 +2346,11 @@ MCPlusBuilder *createRISCVMCPlusBuilder(const MCInstrAnalysis *,
                                         const MCRegisterInfo *,
                                         const MCSubtargetInfo *);
 
+MCPlusBuilder *createPowerPCMCPlusBuilder(const MCInstrAnalysis *,
+                                        const MCInstrInfo *,
+                                        const MCRegisterInfo *,
+                                        const MCSubtargetInfo *);
+
 } // namespace bolt
 } // namespace llvm
 

>From aff001a46f2caa837c490bb0670dfc09185e667c Mon Sep 17 00:00:00 2001
From: Kostas Alvertis <konstantinos.alvertis at gmail.com>
Date: Tue, 20 May 2025 19:57:20 +0100
Subject: [PATCH 07/19] [PPC][BOLT] Update selection logic to invoke the PCC
 factory when PCC architecture is selected

---
 bolt/lib/Rewrite/RewriteInstance.cpp | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/bolt/lib/Rewrite/RewriteInstance.cpp b/bolt/lib/Rewrite/RewriteInstance.cpp
index 4f5a75b770ce6..b6ed356422abb 100644
--- a/bolt/lib/Rewrite/RewriteInstance.cpp
+++ b/bolt/lib/Rewrite/RewriteInstance.cpp
@@ -334,6 +334,11 @@ MCPlusBuilder *createMCPlusBuilder(const Triple::ArchType Arch,
     return createRISCVMCPlusBuilder(Analysis, Info, RegInfo, STI);
 #endif
 
+#ifdef POWERPC_AVAILABLE
+  if (Arch == Triple::ppc64 || Arch == Triple::ppc64le)
+    return createPowerPCMCPlusBuilder(Analysis, Info, RegInfo, STI);
+#endif
+
   llvm_unreachable("architecture unsupported by MCPlusBuilder");
 }
 

>From 9d92487e382aae1f247df7f649c8557381bfe885 Mon Sep 17 00:00:00 2001
From: Kostas Alvertis <konstantinos.alvertis at gmail.com>
Date: Tue, 20 May 2025 20:59:34 +0100
Subject: [PATCH 08/19] [PPC][BOLT] Define macro for POWERPC_AVAILABLE

---
 bolt/lib/Target/CMakeLists.txt | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/bolt/lib/Target/CMakeLists.txt b/bolt/lib/Target/CMakeLists.txt
index eae8ebdddbf3f..38d423ac9483c 100644
--- a/bolt/lib/Target/CMakeLists.txt
+++ b/bolt/lib/Target/CMakeLists.txt
@@ -1,3 +1,5 @@
 foreach (tgt ${BOLT_TARGETS_TO_BUILD})
   add_subdirectory(${tgt})
-endforeach()
+  string(TOUPPER ${tgt} TGT_UPPER)
+  add_definitions(-D${TGT_UPPER}_AVAILABLE)
+endforeach()
\ No newline at end of file

>From 984bcb7e62093714ccbfcc70b8be36b3e7b28c89 Mon Sep 17 00:00:00 2001
From: Kostas Alvertis <konstantinos.alvertis at gmail.com>
Date: Tue, 20 May 2025 22:05:45 +0100
Subject: [PATCH 09/19] [PPC][BOLT]Fixing build

---
 bolt/lib/Target/PowerPC/CMakeLists.txt      | 2 +-
 bolt/lib/Target/PowerPC/PPCMCSymbolizer.cpp | 7 -------
 bolt/lib/Target/PowerPC/PPCMCSymbolizer.h   | 7 -------
 3 files changed, 1 insertion(+), 15 deletions(-)
 delete mode 100644 bolt/lib/Target/PowerPC/PPCMCSymbolizer.cpp
 delete mode 100644 bolt/lib/Target/PowerPC/PPCMCSymbolizer.h

diff --git a/bolt/lib/Target/PowerPC/CMakeLists.txt b/bolt/lib/Target/PowerPC/CMakeLists.txt
index cafb951702cae..2276ced53a3a1 100644
--- a/bolt/lib/Target/PowerPC/CMakeLists.txt
+++ b/bolt/lib/Target/PowerPC/CMakeLists.txt
@@ -1,3 +1,3 @@
-add_llvm_library(BoltPPC
+add_llvm_library(LLVMBOLTTargetPowerPC
     PPCMCPlusBuilder.cpp
   )
\ No newline at end of file
diff --git a/bolt/lib/Target/PowerPC/PPCMCSymbolizer.cpp b/bolt/lib/Target/PowerPC/PPCMCSymbolizer.cpp
deleted file mode 100644
index c5edc77e191f3..0000000000000
--- a/bolt/lib/Target/PowerPC/PPCMCSymbolizer.cpp
+++ /dev/null
@@ -1,7 +0,0 @@
-//===- bolt/Target/PowerPC/PPCMCSymbolizer.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
-//
-//===----------------------------------------------------------------------===//
\ No newline at end of file
diff --git a/bolt/lib/Target/PowerPC/PPCMCSymbolizer.h b/bolt/lib/Target/PowerPC/PPCMCSymbolizer.h
deleted file mode 100644
index b4bfe7e1593c9..0000000000000
--- a/bolt/lib/Target/PowerPC/PPCMCSymbolizer.h
+++ /dev/null
@@ -1,7 +0,0 @@
-//===- bolt/Target/PowerPC/PPCMCSymbolizer.cpp --------------*- C++ -*-===//
-//
-// 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
-//
-//===----------------------------------------------------------------------===//
\ No newline at end of file

>From e411ce10161703fa48af607bd9784519210ee1cf Mon Sep 17 00:00:00 2001
From: Kostas Alvertis <konstantinos.alvertis at gmail.com>
Date: Tue, 20 May 2025 22:20:11 +0100
Subject: [PATCH 10/19] [PPC][BOLT] Fix deprecated include

---
 bolt/lib/Target/PowerPC/PPCMCPlusBuilder.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/bolt/lib/Target/PowerPC/PPCMCPlusBuilder.cpp b/bolt/lib/Target/PowerPC/PPCMCPlusBuilder.cpp
index a43ddd4bfc413..a47cd409848c2 100644
--- a/bolt/lib/Target/PowerPC/PPCMCPlusBuilder.cpp
+++ b/bolt/lib/Target/PowerPC/PPCMCPlusBuilder.cpp
@@ -12,7 +12,7 @@
 
 #include "bolt/Core/MCPlusBuilder.h"
 #include "llvm/MC/MCInst.h"
-#include "llvm/MC/MCPhysReg.h"
+#include "llvm/MC/MCRegisterInfo.h"
 #include "llvm/Target/PowerPC/PPCInstrInfo.h"
 #include "llvm/Target/PowerPC/PPCRegisterInfo.h"
 

>From a047b65236d54d54430439171fe2ded8753f8abf Mon Sep 17 00:00:00 2001
From: Kostas Alvertis <konstantinos.alvertis at gmail.com>
Date: Tue, 20 May 2025 22:34:42 +0100
Subject: [PATCH 11/19] [PPC][BOLT] Cleaning includes to fix build error

---
 bolt/lib/Target/PowerPC/PPCMCPlusBuilder.cpp | 2 --
 1 file changed, 2 deletions(-)

diff --git a/bolt/lib/Target/PowerPC/PPCMCPlusBuilder.cpp b/bolt/lib/Target/PowerPC/PPCMCPlusBuilder.cpp
index a47cd409848c2..6b972f4c4c3ff 100644
--- a/bolt/lib/Target/PowerPC/PPCMCPlusBuilder.cpp
+++ b/bolt/lib/Target/PowerPC/PPCMCPlusBuilder.cpp
@@ -13,8 +13,6 @@
 #include "bolt/Core/MCPlusBuilder.h"
 #include "llvm/MC/MCInst.h"
 #include "llvm/MC/MCRegisterInfo.h"
-#include "llvm/Target/PowerPC/PPCInstrInfo.h"
-#include "llvm/Target/PowerPC/PPCRegisterInfo.h"
 
 namespace llvm {
 namespace bolt {

>From 1009b3d99246076cc985366aa9d1a90ff11f2190 Mon Sep 17 00:00:00 2001
From: Kostas Alvertis <konstantinos.alvertis at gmail.com>
Date: Tue, 20 May 2025 22:46:48 +0100
Subject: [PATCH 12/19] Fix build error, include generated headers, to resolve
 the undeclared opcode enums

---
 bolt/lib/Target/PowerPC/PPCMCPlusBuilder.cpp | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/bolt/lib/Target/PowerPC/PPCMCPlusBuilder.cpp b/bolt/lib/Target/PowerPC/PPCMCPlusBuilder.cpp
index 6b972f4c4c3ff..179f4625e5435 100644
--- a/bolt/lib/Target/PowerPC/PPCMCPlusBuilder.cpp
+++ b/bolt/lib/Target/PowerPC/PPCMCPlusBuilder.cpp
@@ -13,6 +13,8 @@
 #include "bolt/Core/MCPlusBuilder.h"
 #include "llvm/MC/MCInst.h"
 #include "llvm/MC/MCRegisterInfo.h"
+#include "llvm/Target/PowerPC/PPCGenInstrInfo.inc"
+#include "llvm/Target/PowerPC/PPCGenRegisterInfo.inc"
 
 namespace llvm {
 namespace bolt {

>From 70ca20469421793f749a148d3d03aa51876c8aa3 Mon Sep 17 00:00:00 2001
From: Kostas Alvertis <konstantinos.alvertis at gmail.com>
Date: Tue, 20 May 2025 23:25:16 +0100
Subject: [PATCH 13/19] Fix build error, include generated headers

---
 bolt/lib/Target/PowerPC/CMakeLists.txt | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/bolt/lib/Target/PowerPC/CMakeLists.txt b/bolt/lib/Target/PowerPC/CMakeLists.txt
index 2276ced53a3a1..5611cec22210b 100644
--- a/bolt/lib/Target/PowerPC/CMakeLists.txt
+++ b/bolt/lib/Target/PowerPC/CMakeLists.txt
@@ -1,3 +1,8 @@
 add_llvm_library(LLVMBOLTTargetPowerPC
     PPCMCPlusBuilder.cpp
-  )
\ No newline at end of file
+  )
+
+target_include_directories(LLVMBOLTTargetPowerPC PRIVATE
+    ${LLVM_BINARY_DIR}/include
+    ${LLVM_SOURCE_DIR}/include
+)
\ No newline at end of file

>From e48ea5473e33b2f2a9d772d6283e79f31878f6de Mon Sep 17 00:00:00 2001
From: Kostas Alvertis <konstantinos.alvertis at gmail.com>
Date: Wed, 21 May 2025 00:03:18 +0100
Subject: [PATCH 14/19] Fix build error, include generated headers

---
 bolt/lib/Target/PowerPC/CMakeLists.txt | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/bolt/lib/Target/PowerPC/CMakeLists.txt b/bolt/lib/Target/PowerPC/CMakeLists.txt
index 5611cec22210b..80dd29a58b355 100644
--- a/bolt/lib/Target/PowerPC/CMakeLists.txt
+++ b/bolt/lib/Target/PowerPC/CMakeLists.txt
@@ -1,8 +1,16 @@
 add_llvm_library(LLVMBOLTTargetPowerPC
     PPCMCPlusBuilder.cpp
-  )
+)
 
 target_include_directories(LLVMBOLTTargetPowerPC PRIVATE
     ${LLVM_BINARY_DIR}/include
     ${LLVM_SOURCE_DIR}/include
+)
+
+file(MAKE_DIRECTORY "${LLVM_BINARY_DIR}/include/llvm/Target/PowerPC")
+
+add_custom_command(TARGET LLVMBOLTTargetPowerPC POST_BUILD
+    COMMAND ${CMAKE_COMMAND} -E copy_if_different
+        "${LLVM_BINARY_DIR}/lib/Target/PowerPC/PPCGenInstrInfo.inc"
+        "${LLVM_BINARY_DIR}/include/llvm/Target/PowerPC/PPCGenInstrInfo.inc"
 )
\ No newline at end of file

>From 9a2f618be32f8d07512aed950156e5d09d3db6a1 Mon Sep 17 00:00:00 2001
From: Kostas Alvertis <konstantinos.alvertis at gmail.com>
Date: Wed, 21 May 2025 08:53:32 +0100
Subject: [PATCH 15/19] [PPC][BOLT] Fix build error, copy PPGenInstrInfo.inc to
 include dir as soon as it is generated, before any compilation step tries to
 include it

---
 bolt/lib/Target/PowerPC/CMakeLists.txt | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)

diff --git a/bolt/lib/Target/PowerPC/CMakeLists.txt b/bolt/lib/Target/PowerPC/CMakeLists.txt
index 80dd29a58b355..6a4df66e554aa 100644
--- a/bolt/lib/Target/PowerPC/CMakeLists.txt
+++ b/bolt/lib/Target/PowerPC/CMakeLists.txt
@@ -7,10 +7,24 @@ target_include_directories(LLVMBOLTTargetPowerPC PRIVATE
     ${LLVM_SOURCE_DIR}/include
 )
 
+# Ensure the destination directory exists
 file(MAKE_DIRECTORY "${LLVM_BINARY_DIR}/include/llvm/Target/PowerPC")
 
-add_custom_command(TARGET LLVMBOLTTargetPowerPC POST_BUILD
+# Custom command to copy the .inc file when it's generated
+add_custom_command(
+    OUTPUT "${LLVM_BINARY_DIR}/include/llvm/Target/PowerPC/PPCGenInstrInfo.inc"
     COMMAND ${CMAKE_COMMAND} -E copy_if_different
         "${LLVM_BINARY_DIR}/lib/Target/PowerPC/PPCGenInstrInfo.inc"
         "${LLVM_BINARY_DIR}/include/llvm/Target/PowerPC/PPCGenInstrInfo.inc"
-)
\ No newline at end of file
+    DEPENDS "${LLVM_BINARY_DIR}/lib/Target/PowerPC/PPCGenInstrInfo.inc"
+    COMMENT "Copying PPCGenInstrInfo.inc to include directory"
+)
+
+# Custom target to represent the copied .inc file
+add_custom_target(
+    BoltCopyPPCGenInstrInfoInc ALL
+    DEPENDS "${LLVM_BINARY_DIR}/include/llvm/Target/PowerPC/PPCGenInstrInfo.inc"
+)
+
+# Make your library depend on the copy
+add_dependencies(LLVMBOLTTargetPowerPC BoltCopyPPCGenInstrInfoInc)
\ No newline at end of file

>From e3847ac07c210a7219236b547070a70f18efa33c Mon Sep 17 00:00:00 2001
From: Kostas Alvertis <konstantinos.alvertis at gmail.com>
Date: Wed, 21 May 2025 09:09:43 +0100
Subject: [PATCH 16/19] [PPC][BOLT] Fix build error, copy
 PPCGenRegisterInfo.inc to include dir as soon as it is generated, before any
 compilation step tries to include it

---
 bolt/lib/Target/PowerPC/CMakeLists.txt | 35 +++++++++++++-------------
 1 file changed, 17 insertions(+), 18 deletions(-)

diff --git a/bolt/lib/Target/PowerPC/CMakeLists.txt b/bolt/lib/Target/PowerPC/CMakeLists.txt
index 6a4df66e554aa..c1d2a054396d7 100644
--- a/bolt/lib/Target/PowerPC/CMakeLists.txt
+++ b/bolt/lib/Target/PowerPC/CMakeLists.txt
@@ -7,24 +7,23 @@ target_include_directories(LLVMBOLTTargetPowerPC PRIVATE
     ${LLVM_SOURCE_DIR}/include
 )
 
-# Ensure the destination directory exists
 file(MAKE_DIRECTORY "${LLVM_BINARY_DIR}/include/llvm/Target/PowerPC")
 
-# Custom command to copy the .inc file when it's generated
-add_custom_command(
-    OUTPUT "${LLVM_BINARY_DIR}/include/llvm/Target/PowerPC/PPCGenInstrInfo.inc"
-    COMMAND ${CMAKE_COMMAND} -E copy_if_different
-        "${LLVM_BINARY_DIR}/lib/Target/PowerPC/PPCGenInstrInfo.inc"
-        "${LLVM_BINARY_DIR}/include/llvm/Target/PowerPC/PPCGenInstrInfo.inc"
-    DEPENDS "${LLVM_BINARY_DIR}/lib/Target/PowerPC/PPCGenInstrInfo.inc"
-    COMMENT "Copying PPCGenInstrInfo.inc to include directory"
+foreach(incfile IN ITEMS
+    PPCGenInstrInfo.inc
+    PPCGenRegisterInfo.inc
 )
-
-# Custom target to represent the copied .inc file
-add_custom_target(
-    BoltCopyPPCGenInstrInfoInc ALL
-    DEPENDS "${LLVM_BINARY_DIR}/include/llvm/Target/PowerPC/PPCGenInstrInfo.inc"
-)
-
-# Make your library depend on the copy
-add_dependencies(LLVMBOLTTargetPowerPC BoltCopyPPCGenInstrInfoInc)
\ No newline at end of file
+    add_custom_command(
+        OUTPUT "${LLVM_BINARY_DIR}/include/llvm/Target/PowerPC/${incfile}"
+        COMMAND ${CMAKE_COMMAND} -E copy_if_different
+            "${LLVM_BINARY_DIR}/lib/Target/PowerPC/${incfile}"
+            "${LLVM_BINARY_DIR}/include/llvm/Target/PowerPC/${incfile}"
+        DEPENDS "${LLVM_BINARY_DIR}/lib/Target/PowerPC/${incfile}"
+        COMMENT "Copying ${incfile} to include directory"
+    )
+    add_custom_target(
+        "BoltCopy${incfile}" ALL
+        DEPENDS "${LLVM_BINARY_DIR}/include/llvm/Target/PowerPC/${incfile}"
+    )
+    add_dependencies(LLVMBOLTTargetPowerPC "BoltCopy${incfile}")
+endforeach()
\ No newline at end of file

>From efed56d2444b085aa821ece95c11d6a6995c76a5 Mon Sep 17 00:00:00 2001
From: Kostas Alvertis <konstantinos.alvertis at gmail.com>
Date: Wed, 21 May 2025 09:57:01 +0100
Subject: [PATCH 17/19] [PPC][BOLT] Make enums STDU, PCC visible

---
 bolt/lib/Target/PowerPC/PPCMCPlusBuilder.cpp | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/bolt/lib/Target/PowerPC/PPCMCPlusBuilder.cpp b/bolt/lib/Target/PowerPC/PPCMCPlusBuilder.cpp
index 179f4625e5435..0bbbef985264e 100644
--- a/bolt/lib/Target/PowerPC/PPCMCPlusBuilder.cpp
+++ b/bolt/lib/Target/PowerPC/PPCMCPlusBuilder.cpp
@@ -13,7 +13,9 @@
 #include "bolt/Core/MCPlusBuilder.h"
 #include "llvm/MC/MCInst.h"
 #include "llvm/MC/MCRegisterInfo.h"
+#define GET_INSTRINFO_ENUM
 #include "llvm/Target/PowerPC/PPCGenInstrInfo.inc"
+#define GET_REGINFO_ENUM
 #include "llvm/Target/PowerPC/PPCGenRegisterInfo.inc"
 
 namespace llvm {

>From 901bf6fa2ab6747e3fc8b774bd39b643dc156e83 Mon Sep 17 00:00:00 2001
From: Kostas <konstantinos.alvertis at gmail.com>
Date: Wed, 21 May 2025 17:45:19 +0100
Subject: [PATCH 18/19] [PPC][BOLT] clang-format

---
 bolt/include/bolt/Core/MCPlusBuilder.h       |  6 ++--
 bolt/lib/Target/PowerPC/PPCMCPlusBuilder.cpp | 38 ++++++++++----------
 2 files changed, 22 insertions(+), 22 deletions(-)

diff --git a/bolt/include/bolt/Core/MCPlusBuilder.h b/bolt/include/bolt/Core/MCPlusBuilder.h
index 32ccc21a4be59..c4ac81f88600b 100644
--- a/bolt/include/bolt/Core/MCPlusBuilder.h
+++ b/bolt/include/bolt/Core/MCPlusBuilder.h
@@ -2347,9 +2347,9 @@ MCPlusBuilder *createRISCVMCPlusBuilder(const MCInstrAnalysis *,
                                         const MCSubtargetInfo *);
 
 MCPlusBuilder *createPowerPCMCPlusBuilder(const MCInstrAnalysis *,
-                                        const MCInstrInfo *,
-                                        const MCRegisterInfo *,
-                                        const MCSubtargetInfo *);
+                                          const MCInstrInfo *,
+                                          const MCRegisterInfo *,
+                                          const MCSubtargetInfo *);
 
 } // namespace bolt
 } // namespace llvm
diff --git a/bolt/lib/Target/PowerPC/PPCMCPlusBuilder.cpp b/bolt/lib/Target/PowerPC/PPCMCPlusBuilder.cpp
index 0bbbef985264e..39d5ed2d3e36e 100644
--- a/bolt/lib/Target/PowerPC/PPCMCPlusBuilder.cpp
+++ b/bolt/lib/Target/PowerPC/PPCMCPlusBuilder.cpp
@@ -21,25 +21,26 @@
 namespace llvm {
 namespace bolt {
 
-class PPCMCPlusBuilder : public MCPlusBuilder{
+class PPCMCPlusBuilder : public MCPlusBuilder {
 public:
-    using MCPlusBuilder::MCPlusBuilder;
-
-    // Create instructions to push two registers onto the stack
-    static void createPushRegisters(MCInst &Inst1, MCInst &Inst2, MCPhysReg Reg1, MCPhysReg /*Reg2*/){
-
-        Inst1.clear();
-        Inst1.setOpcode(PPC::STDU);
-        Inst1.addOperand(MCOperand::createReg(PPC::R1)); // destination (SP)
-        Inst1.addOperand(MCOperand::createReg(PPC::R1)); // base (SP)
-        Inst1.addOperand(MCOperand::createImm(-16));     // offset
-
-        Inst2.clear();
-        Inst2.setOpcode(PPC::STD);
-        Inst2.addOperand(MCOperand::createReg(Reg1));     // source register
-        Inst2.addOperand(MCOperand::createReg(PPC::R1));  // base (SP)
-        Inst2.addOperand(MCOperand::createImm(0));        // offset
-    }
+  using MCPlusBuilder::MCPlusBuilder;
+
+  // Create instructions to push two registers onto the stack
+  static void createPushRegisters(MCInst &Inst1, MCInst &Inst2, MCPhysReg Reg1,
+                                  MCPhysReg /*Reg2*/) {
+
+    Inst1.clear();
+    Inst1.setOpcode(PPC::STDU);
+    Inst1.addOperand(MCOperand::createReg(PPC::R1)); // destination (SP)
+    Inst1.addOperand(MCOperand::createReg(PPC::R1)); // base (SP)
+    Inst1.addOperand(MCOperand::createImm(-16));     // offset
+
+    Inst2.clear();
+    Inst2.setOpcode(PPC::STD);
+    Inst2.addOperand(MCOperand::createReg(Reg1));    // source register
+    Inst2.addOperand(MCOperand::createReg(PPC::R1)); // base (SP)
+    Inst2.addOperand(MCOperand::createImm(0));       // offset
+  }
 };
 
 MCPlusBuilder *createPowerPCMCPlusBuilder(const MCInstrAnalysis *Analysis,
@@ -51,4 +52,3 @@ MCPlusBuilder *createPowerPCMCPlusBuilder(const MCInstrAnalysis *Analysis,
 
 } // namespace bolt
 } // namespace llvm
-

>From 8f0dfc1e0b0df509387596586f20e37a69fa5833 Mon Sep 17 00:00:00 2001
From: Kostas Alvertis <konstantinos.alvertis at gmail.com>
Date: Tue, 26 Aug 2025 18:10:54 +0100
Subject: [PATCH 19/19] [PPC][BOLT] Use PPCMCTargetDesc.h to include generated
 .inc files.

Replace direct inclusion of PPCGenInstrInfo.inc and PPCGenRegisterInfo.inc with PPCMCTargetDesc.h, which pulls in the necessary enums. This aligns the PowerPC target with the AArch64 and X86 patterns and follows the guidance from initial code review.
---
 bolt/lib/Target/PowerPC/CMakeLists.txt       | 50 +++++++++++---------
 bolt/lib/Target/PowerPC/PPCMCPlusBuilder.cpp |  5 +-
 2 files changed, 28 insertions(+), 27 deletions(-)

diff --git a/bolt/lib/Target/PowerPC/CMakeLists.txt b/bolt/lib/Target/PowerPC/CMakeLists.txt
index c1d2a054396d7..eebb8120a1644 100644
--- a/bolt/lib/Target/PowerPC/CMakeLists.txt
+++ b/bolt/lib/Target/PowerPC/CMakeLists.txt
@@ -1,29 +1,33 @@
-add_llvm_library(LLVMBOLTTargetPowerPC
-    PPCMCPlusBuilder.cpp
+set(LLVM_LINK_COMPONENTS
+  MC
+  MCDisassembler
+  Support
+  PowerPCDesc
 )
 
-target_include_directories(LLVMBOLTTargetPowerPC PRIVATE
-    ${LLVM_BINARY_DIR}/include
-    ${LLVM_SOURCE_DIR}/include
+if(BOLT_BUILT_STANDALONE)
+  set(LLVM_TARGET_DEFINITIONS ${LLVM_MAIN_SRC_DIR}/lib/Target/PowerPC/PPC.td)
+  list(APPEND LLVM_TABLEGEN_FLAGS -I ${LLVM_MAIN_SRC_DIR}/lib/Target/PowerPC)
+  tablegen(LLVM PPCGenInstrInfo.inc      -gen-instr-info)
+  tablegen(LLVM PPCGenRegisterInfo.inc   -gen-register-info)
+  tablegen(LLVM PPCGenSubtargetInfo.inc  -gen-subtarget)
+  add_public_tablegen_target(PowerPCCommonTableGen)
+  include_directories(${CMAKE_CURRENT_BINARY_DIR})
+endif()
+
+add_llvm_library(LLVMBOLTTargetPowerPC
+  PPCMCPlusBuilder.cpp
+
+  NO_EXPORT
+  DISABLE_LLVM_LINK_LLVM_DYLIB
+
+  DEPENDS
+  PowerPCCommonTableGen
 )
 
-file(MAKE_DIRECTORY "${LLVM_BINARY_DIR}/include/llvm/Target/PowerPC")
+target_link_libraries(LLVMBOLTTargetPowerPC PRIVATE LLVMBOLTCore)
 
-foreach(incfile IN ITEMS
-    PPCGenInstrInfo.inc
-    PPCGenRegisterInfo.inc
+include_directories(
+  ${LLVM_MAIN_SRC_DIR}/lib/Target/PowerPC
+  ${LLVM_BINARY_DIR}/lib/Target/PowerPC
 )
-    add_custom_command(
-        OUTPUT "${LLVM_BINARY_DIR}/include/llvm/Target/PowerPC/${incfile}"
-        COMMAND ${CMAKE_COMMAND} -E copy_if_different
-            "${LLVM_BINARY_DIR}/lib/Target/PowerPC/${incfile}"
-            "${LLVM_BINARY_DIR}/include/llvm/Target/PowerPC/${incfile}"
-        DEPENDS "${LLVM_BINARY_DIR}/lib/Target/PowerPC/${incfile}"
-        COMMENT "Copying ${incfile} to include directory"
-    )
-    add_custom_target(
-        "BoltCopy${incfile}" ALL
-        DEPENDS "${LLVM_BINARY_DIR}/include/llvm/Target/PowerPC/${incfile}"
-    )
-    add_dependencies(LLVMBOLTTargetPowerPC "BoltCopy${incfile}")
-endforeach()
\ No newline at end of file
diff --git a/bolt/lib/Target/PowerPC/PPCMCPlusBuilder.cpp b/bolt/lib/Target/PowerPC/PPCMCPlusBuilder.cpp
index 39d5ed2d3e36e..bab54e5f06a80 100644
--- a/bolt/lib/Target/PowerPC/PPCMCPlusBuilder.cpp
+++ b/bolt/lib/Target/PowerPC/PPCMCPlusBuilder.cpp
@@ -10,13 +10,10 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "MCTargetDesc/PPCMCTargetDesc.h"
 #include "bolt/Core/MCPlusBuilder.h"
 #include "llvm/MC/MCInst.h"
 #include "llvm/MC/MCRegisterInfo.h"
-#define GET_INSTRINFO_ENUM
-#include "llvm/Target/PowerPC/PPCGenInstrInfo.inc"
-#define GET_REGINFO_ENUM
-#include "llvm/Target/PowerPC/PPCGenRegisterInfo.inc"
 
 namespace llvm {
 namespace bolt {



More information about the llvm-commits mailing list