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

via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 12 16:10:57 PDT 2025


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

>From 9445f25a4b78a176cee092e4c2d3482595b9a543 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 88d73043a23ca96493d7b5164a597049cb2dc66c 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 eb6e9a5bd1b9aa91636bb047d55836c3016f64fc 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 6c1c3c1a9c6998a7308d2609a40344a22dbc61fe 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 74dd5f583f6dfe868b0a6763da3118dd75a6008b 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 ce7fdba5549971bdcbc7432093f4d8a82f2e5ac6 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 804100db80793..7caa41989d945 100644
--- a/bolt/include/bolt/Core/MCPlusBuilder.h
+++ b/bolt/include/bolt/Core/MCPlusBuilder.h
@@ -2317,6 +2317,11 @@ MCPlusBuilder *createRISCVMCPlusBuilder(const MCInstrAnalysis *,
                                         const MCRegisterInfo *,
                                         const MCSubtargetInfo *);
 
+MCPlusBuilder *createPowerPCMCPlusBuilder(const MCInstrAnalysis *,
+                                        const MCInstrInfo *,
+                                        const MCRegisterInfo *,
+                                        const MCSubtargetInfo *);
+
 } // namespace bolt
 } // namespace llvm
 

>From 5d88b576a122604611d69d576b2b444f54b80570 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 e1aa00a3d749f..b8fc4fd850183 100644
--- a/bolt/lib/Rewrite/RewriteInstance.cpp
+++ b/bolt/lib/Rewrite/RewriteInstance.cpp
@@ -304,6 +304,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 b22925a9ff7bd35928f7cd46e6e8f353d1f8ebce 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 f038d219ac99b98c322fe036031eefcb6d167fcc 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 42ef7dd07b7726cc917b2d7ccc4554fc5bbc0651 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 b3db5b289ebc0f1fd7692e651d9fe7e0e4606d96 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 82d1b9621d897c0d6accea157d7afb16094abcc2 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 3b5dee403e202c9cf03d4b77dd88c1ce6cc9fd60 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 c7c7e2cfc35cc7c391a72ea6a3262a1a84f90b36 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 1af6a721a0132046849cd815461f323031d88729 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 67215fdd1eb9e1435df7ab706198b51396bbd806 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 78050b34010f2fc40792a36e4332e643a528e6b4 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 1e2b9d7a40fde1dc7266d566ed73d76e2d717e81 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 7caa41989d945..6809f93948eb8 100644
--- a/bolt/include/bolt/Core/MCPlusBuilder.h
+++ b/bolt/include/bolt/Core/MCPlusBuilder.h
@@ -2318,9 +2318,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 adad2ed2c9df762ec21f6286306627fc534beea7 Mon Sep 17 00:00:00 2001
From: Kostas <konstantinos.alvertis at gmail.com>
Date: Thu, 12 Jun 2025 20:40:33 +0100
Subject: [PATCH 19/19] Add unit test

---
 .../bolt/Target/PowerPC/PPCMCPlusBuilder.h    | 16 ++++++
 bolt/lib/Target/PowerPC/PPCMCPlusBuilder.cpp  |  9 +---
 bolt/unittests/CMakeLists.txt                 |  1 +
 bolt/unittests/Target/CMakeLists.txt          |  1 +
 bolt/unittests/Target/PowerPC/CMakeLists.txt  | 20 +++++++
 .../Target/PowerPC/PPCMCPlusBuilderTest.cpp   | 53 +++++++++++++++++++
 6 files changed, 93 insertions(+), 7 deletions(-)
 create mode 100644 bolt/include/bolt/Target/PowerPC/PPCMCPlusBuilder.h
 create mode 100644 bolt/unittests/Target/CMakeLists.txt
 create mode 100644 bolt/unittests/Target/PowerPC/CMakeLists.txt
 create mode 100644 bolt/unittests/Target/PowerPC/PPCMCPlusBuilderTest.cpp

diff --git a/bolt/include/bolt/Target/PowerPC/PPCMCPlusBuilder.h b/bolt/include/bolt/Target/PowerPC/PPCMCPlusBuilder.h
new file mode 100644
index 0000000000000..20750ff68b626
--- /dev/null
+++ b/bolt/include/bolt/Target/PowerPC/PPCMCPlusBuilder.h
@@ -0,0 +1,16 @@
+#pragma once
+
+#include "bolt/Core/MCPlusBuilder.h"
+
+namespace llvm{
+namespace bolt{
+
+class PPCMCPlusBuilder : public MCPlusBuilder{
+public:
+    using MCPlusBuilder::MCPlusBuilder;
+    
+    static void createPushRegisters(MCInst &Inst1, MCInst &Inst2, MCPhysReg Reg1, MCPhysReg Reg2);
+};
+
+} // namespace bolt
+} // namespace llvm
\ No newline at end of file
diff --git a/bolt/lib/Target/PowerPC/PPCMCPlusBuilder.cpp b/bolt/lib/Target/PowerPC/PPCMCPlusBuilder.cpp
index 39d5ed2d3e36e..dfdcab79ff0f7 100644
--- a/bolt/lib/Target/PowerPC/PPCMCPlusBuilder.cpp
+++ b/bolt/lib/Target/PowerPC/PPCMCPlusBuilder.cpp
@@ -10,6 +10,7 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "bolt/Target/PowerPC/PPCMCPlusBuilder.h"
 #include "bolt/Core/MCPlusBuilder.h"
 #include "llvm/MC/MCInst.h"
 #include "llvm/MC/MCRegisterInfo.h"
@@ -21,14 +22,9 @@
 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,
+  void PPCMCPlusBuilder::createPushRegisters(MCInst &Inst1, MCInst &Inst2, MCPhysReg Reg1,
                                   MCPhysReg /*Reg2*/) {
-
     Inst1.clear();
     Inst1.setOpcode(PPC::STDU);
     Inst1.addOperand(MCOperand::createReg(PPC::R1)); // destination (SP)
@@ -41,7 +37,6 @@ class PPCMCPlusBuilder : public MCPlusBuilder {
     Inst2.addOperand(MCOperand::createReg(PPC::R1)); // base (SP)
     Inst2.addOperand(MCOperand::createImm(0));       // offset
   }
-};
 
 MCPlusBuilder *createPowerPCMCPlusBuilder(const MCInstrAnalysis *Analysis,
                                           const MCInstrInfo *Info,
diff --git a/bolt/unittests/CMakeLists.txt b/bolt/unittests/CMakeLists.txt
index 64414b83d39fe..7582f5bf80c9e 100644
--- a/bolt/unittests/CMakeLists.txt
+++ b/bolt/unittests/CMakeLists.txt
@@ -7,3 +7,4 @@ endfunction()
 
 add_subdirectory(Core)
 add_subdirectory(Profile)
+add_subdirectory(Target)
\ No newline at end of file
diff --git a/bolt/unittests/Target/CMakeLists.txt b/bolt/unittests/Target/CMakeLists.txt
new file mode 100644
index 0000000000000..6837a2c945fb3
--- /dev/null
+++ b/bolt/unittests/Target/CMakeLists.txt
@@ -0,0 +1 @@
+add_subdirectory(PowerPC)
\ No newline at end of file
diff --git a/bolt/unittests/Target/PowerPC/CMakeLists.txt b/bolt/unittests/Target/PowerPC/CMakeLists.txt
new file mode 100644
index 0000000000000..3aa6c9f866c5f
--- /dev/null
+++ b/bolt/unittests/Target/PowerPC/CMakeLists.txt
@@ -0,0 +1,20 @@
+set(BOLTTargetPowerPCTestsSources
+    PPCMCPlusBuilderTest.cpp)
+
+add_bolt_unittest(BOLTTargetPowerPCTests
+  ${BOLTTargetPowerPCTestsSources}
+)
+
+target_link_libraries(BOLTTargetPowerPCTests PRIVATE
+  LLVMBOLTTargetPowerPC
+  LLVMBOLTCore
+  LLVMCore
+)
+
+target_include_directories(BOLTTargetPowerPCTests PRIVATE
+  ${LLVM_BINARY_DIR}/include
+  ${LLVM_SOURCE_DIR}/include
+  ${LLVM_SOURCE_DIR}/bolt/include
+  ${LLVM_BINARY_DIR}/tools/bolt/include
+  ${CMAKE_SOURCE_DIR}
+)
diff --git a/bolt/unittests/Target/PowerPC/PPCMCPlusBuilderTest.cpp b/bolt/unittests/Target/PowerPC/PPCMCPlusBuilderTest.cpp
new file mode 100644
index 0000000000000..a8d26f58ba695
--- /dev/null
+++ b/bolt/unittests/Target/PowerPC/PPCMCPlusBuilderTest.cpp
@@ -0,0 +1,53 @@
+//===- bolt/unittest/Target/PowerPC/PPCMCPlusBuilderTest.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
+//
+//===----------------------------------------------------------------------===//
+
+#include "bolt/Core/MCPlusBuilder.h"
+#include "bolt/Target/PowerPC/PPCMCPlusBuilder.h"
+#include "llvm/MC/MCInst.h"
+#include "gtest/gtest.h"
+#define GET_INSTRINFO_ENUM
+#include "llvm/Target/PowerPC/PPCGenInstrInfo.inc"
+#define GET_REGINFO_ENUM
+#include "llvm/Target/PowerPC/PPCGenRegisterInfo.inc"
+
+
+using namespace llvm;
+using namespace bolt;
+
+namespace{
+
+TEST(PPCMCPlusBuilderTest, CreatePushRegisters){
+// Set up dummy input registers
+MCInst Inst1, Inst2;
+MCPhysReg Reg1 = PPC::R3; // Arbitary register
+
+// Call the method under test
+PPCMCPlusBuilder::createPushRegisters(Inst1, Inst2, Reg1, /*Reg2=*/PPC::R4);
+
+// Check Inst1 is STDU R1, R1, -16
+EXPECT_EQ(Inst1.getOpcode(),PPC::STDU);
+ASSERT_EQ(Inst1.getNumOperands(),3u);
+EXPECT_TRUE(Inst1.getOperand(0).isReg());
+EXPECT_EQ(Inst1.getOperand(0).getReg(),PPC::R1);
+EXPECT_TRUE(Inst1.getOperand(1).isReg());
+EXPECT_EQ(Inst1.getOperand(1).getReg(),PPC::R1);
+EXPECT_TRUE(Inst1.getOperand(2).isImm());
+EXPECT_EQ(Inst1.getOperand(2).getImm(),-16);
+
+// Check Inst2 is STD Reg1, R1, 0
+EXPECT_EQ(Inst2.getOpcode(), PPC::STD);
+ASSERT_EQ(Inst2.getNumOperands(), 3u);
+EXPECT_TRUE(Inst2.getOperand(0).isReg());
+EXPECT_EQ(Inst2.getOperand(0).getReg(), Reg1);
+EXPECT_TRUE(Inst2.getOperand(1).isReg());
+EXPECT_EQ(Inst2.getOperand(1).getReg(), PPC::R1);
+EXPECT_TRUE(Inst2.getOperand(2).isImm());
+EXPECT_EQ(Inst2.getOperand(2).getImm(),0);
+}
+
+} // end anonymous namespace
\ No newline at end of file



More information about the llvm-commits mailing list