https://github.com/kostasalv updated https://github.com/llvm/llvm-project/pull/140894
>From 92e2fd6cc4ff1b044d5275d50351e51297e870d0 Mon Sep 17 00:00:00 2001 From: Kostas Alvertis <konstantinos.alver...@gmail.com> Date: Tue, 20 May 2025 16:24:48 +0100 Subject: [PATCH 01/21] 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 087c00e409f8427c8e38488c6e9ddbb9bbc1bca1 Mon Sep 17 00:00:00 2001 From: Kostas Alvertis <konstantinos.alver...@gmail.com> Date: Tue, 20 May 2025 17:12:43 +0100 Subject: [PATCH 02/21] [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 ea23773dc72ec6f09c2fe5f68b6fc7ddbaabd217 Mon Sep 17 00:00:00 2001 From: Kostas Alvertis <konstantinos.alver...@gmail.com> Date: Tue, 20 May 2025 18:42:52 +0100 Subject: [PATCH 03/21] [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 d6e90c4458da8eff04b621d92760d81507dd2c7b Mon Sep 17 00:00:00 2001 From: Kostas Alvertis <konstantinos.alver...@gmail.com> Date: Tue, 20 May 2025 18:47:08 +0100 Subject: [PATCH 04/21] [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 f95555abf7947af73ed445594a53d7a39b5f4e7f Mon Sep 17 00:00:00 2001 From: Kostas Alvertis <konstantinos.alver...@gmail.com> Date: Tue, 20 May 2025 19:21:48 +0100 Subject: [PATCH 05/21] [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 7d91a5ef12b7a7bf5d8d12780e0e096e8f5ed323 Mon Sep 17 00:00:00 2001 From: Kostas Alvertis <konstantinos.alver...@gmail.com> Date: Tue, 20 May 2025 19:50:15 +0100 Subject: [PATCH 06/21] [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 90129d475d870..973f72d5e8b95 100644 --- a/bolt/include/bolt/Core/MCPlusBuilder.h +++ b/bolt/include/bolt/Core/MCPlusBuilder.h @@ -2373,6 +2373,11 @@ MCPlusBuilder *createRISCVMCPlusBuilder(const MCInstrAnalysis *, const MCRegisterInfo *, const MCSubtargetInfo *); +MCPlusBuilder *createPowerPCMCPlusBuilder(const MCInstrAnalysis *, + const MCInstrInfo *, + const MCRegisterInfo *, + const MCSubtargetInfo *); + } // namespace bolt } // namespace llvm >From ec46103d5851db113cf3d7d8d223ef30897fcff7 Mon Sep 17 00:00:00 2001 From: Kostas Alvertis <konstantinos.alver...@gmail.com> Date: Tue, 20 May 2025 19:57:20 +0100 Subject: [PATCH 07/21] [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 a6e4dbc9c192f..f196d83587f77 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 cf14cf7cfbb14d74bd5208c66914617c57736848 Mon Sep 17 00:00:00 2001 From: Kostas Alvertis <konstantinos.alver...@gmail.com> Date: Tue, 20 May 2025 20:59:34 +0100 Subject: [PATCH 08/21] [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 252544d02ad44919643a3ee90bea42d6e3cffa7e Mon Sep 17 00:00:00 2001 From: Kostas Alvertis <konstantinos.alver...@gmail.com> Date: Tue, 20 May 2025 22:05:45 +0100 Subject: [PATCH 09/21] [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 e1df96e2d4aea7e646ab1ffcd4dc5e725f56174d Mon Sep 17 00:00:00 2001 From: Kostas Alvertis <konstantinos.alver...@gmail.com> Date: Tue, 20 May 2025 22:20:11 +0100 Subject: [PATCH 10/21] [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 7b0128306ca712e06b69ff5c2a00d230be450390 Mon Sep 17 00:00:00 2001 From: Kostas Alvertis <konstantinos.alver...@gmail.com> Date: Tue, 20 May 2025 22:34:42 +0100 Subject: [PATCH 11/21] [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 bfcb0d683e2f752c2e72e9650afe9cd58e5dbe0e Mon Sep 17 00:00:00 2001 From: Kostas Alvertis <konstantinos.alver...@gmail.com> Date: Tue, 20 May 2025 22:46:48 +0100 Subject: [PATCH 12/21] 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 a52fabeb1a8c24af15161192371e736963d419b4 Mon Sep 17 00:00:00 2001 From: Kostas Alvertis <konstantinos.alver...@gmail.com> Date: Tue, 20 May 2025 23:25:16 +0100 Subject: [PATCH 13/21] 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 4ebd40a936b5b9e70db03dd08471b88a6694dc60 Mon Sep 17 00:00:00 2001 From: Kostas Alvertis <konstantinos.alver...@gmail.com> Date: Wed, 21 May 2025 00:03:18 +0100 Subject: [PATCH 14/21] 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 9457f2b257c22d3b9af436f490a59441b139cb5b Mon Sep 17 00:00:00 2001 From: Kostas Alvertis <konstantinos.alver...@gmail.com> Date: Wed, 21 May 2025 08:53:32 +0100 Subject: [PATCH 15/21] [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 e74c4fa1fb983cd9b40da41b174eb32eedc34d57 Mon Sep 17 00:00:00 2001 From: Kostas Alvertis <konstantinos.alver...@gmail.com> Date: Wed, 21 May 2025 09:09:43 +0100 Subject: [PATCH 16/21] [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 792ed13d136480e3df902e6e573598c63c2d0179 Mon Sep 17 00:00:00 2001 From: Kostas Alvertis <konstantinos.alver...@gmail.com> Date: Wed, 21 May 2025 09:57:01 +0100 Subject: [PATCH 17/21] [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 efd806e039d2cf53af29d993ca915d3bdb672790 Mon Sep 17 00:00:00 2001 From: Kostas <konstantinos.alver...@gmail.com> Date: Wed, 21 May 2025 17:45:19 +0100 Subject: [PATCH 18/21] [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 973f72d5e8b95..1106a6f3f66fe 100644 --- a/bolt/include/bolt/Core/MCPlusBuilder.h +++ b/bolt/include/bolt/Core/MCPlusBuilder.h @@ -2374,9 +2374,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 04e648ba107a3fe7055d6e119239addf4e8151db Mon Sep 17 00:00:00 2001 From: Kostas <konstantinos.alver...@gmail.com> Date: Fri, 13 Jun 2025 00:16:49 +0100 Subject: [PATCH 19/21] [BOLT][PPC] Add unit test. --- .../bolt/Target/PowerPC/PPCMCPlusBuilder.h | 17 ++++++ bolt/lib/Target/PowerPC/PPCMCPlusBuilder.cpp | 37 ++++++------- 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, 108 insertions(+), 21 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..570c38a99d0c6 --- /dev/null +++ b/bolt/include/bolt/Target/PowerPC/PPCMCPlusBuilder.h @@ -0,0 +1,17 @@ +#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..096ef106f3e16 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,27 +22,21 @@ 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 - } -}; +// Create instructions to push two registers onto the stack +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) + 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, 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..e069ca4b618f6 --- /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/Target/PowerPC/PPCMCPlusBuilder.h" +#include "bolt/Core/MCPlusBuilder.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 >From 4b547ad7df124d90568b8b3136953086ebf81f10 Mon Sep 17 00:00:00 2001 From: Kostas <konstantinos.alver...@gmail.com> Date: Tue, 26 Aug 2025 21:30:31 +0100 Subject: [PATCH 20/21] [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 | 16 +++--- bolt/unittests/Target/PowerPC/CMakeLists.txt | 2 + .../Target/PowerPC/PPCMCPlusBuilderTest.cpp | 5 +- 4 files changed, 38 insertions(+), 35 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 096ef106f3e16..7e42376ce1ca9 100644 --- a/bolt/lib/Target/PowerPC/PPCMCPlusBuilder.cpp +++ b/bolt/lib/Target/PowerPC/PPCMCPlusBuilder.cpp @@ -11,20 +11,17 @@ //===----------------------------------------------------------------------===// #include "bolt/Target/PowerPC/PPCMCPlusBuilder.h" -#include "bolt/Core/MCPlusBuilder.h" +#include "MCTargetDesc/PPCMCTargetDesc.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 { +using namespace llvm; +using namespace bolt; // Create instructions to push two registers onto the stack 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) @@ -38,6 +35,9 @@ void PPCMCPlusBuilder::createPushRegisters(MCInst &Inst1, MCInst &Inst2, Inst2.addOperand(MCOperand::createImm(0)); // offset } +namespace llvm { +namespace bolt { + MCPlusBuilder *createPowerPCMCPlusBuilder(const MCInstrAnalysis *Analysis, const MCInstrInfo *Info, const MCRegisterInfo *RegInfo, @@ -46,4 +46,4 @@ MCPlusBuilder *createPowerPCMCPlusBuilder(const MCInstrAnalysis *Analysis, } } // namespace bolt -} // namespace llvm +} // namespace llvm \ No newline at end of file diff --git a/bolt/unittests/Target/PowerPC/CMakeLists.txt b/bolt/unittests/Target/PowerPC/CMakeLists.txt index 3aa6c9f866c5f..182d6a78456e2 100644 --- a/bolt/unittests/Target/PowerPC/CMakeLists.txt +++ b/bolt/unittests/Target/PowerPC/CMakeLists.txt @@ -17,4 +17,6 @@ target_include_directories(BOLTTargetPowerPCTests PRIVATE ${LLVM_SOURCE_DIR}/bolt/include ${LLVM_BINARY_DIR}/tools/bolt/include ${CMAKE_SOURCE_DIR} + ${LLVM_MAIN_SRC_DIR}/lib/Target/PowerPC + ${LLVM_BINARY_DIR}/lib/Target/PowerPC ) diff --git a/bolt/unittests/Target/PowerPC/PPCMCPlusBuilderTest.cpp b/bolt/unittests/Target/PowerPC/PPCMCPlusBuilderTest.cpp index e069ca4b618f6..787257e0a2f3b 100644 --- a/bolt/unittests/Target/PowerPC/PPCMCPlusBuilderTest.cpp +++ b/bolt/unittests/Target/PowerPC/PPCMCPlusBuilderTest.cpp @@ -8,13 +8,10 @@ //===----------------------------------------------------------------------===// #include "bolt/Target/PowerPC/PPCMCPlusBuilder.h" +#include "MCTargetDesc/PPCMCTargetDesc.h" #include "bolt/Core/MCPlusBuilder.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; >From 1b041ffcc0883f97dcbeab10f0c0dc8dac128f85 Mon Sep 17 00:00:00 2001 From: Kostas Alvertis <konstantinos.alver...@gmail.com> Date: Fri, 19 Sep 2025 18:36:04 +0100 Subject: [PATCH 21/21] [PPC][BOLT] Add minimal PowerPC MCSymbolizer. Introduce PPCMCSymbolizer. This initial version supports resolving relocations by printing symbolic names and attaching them to instructions. It enables BOLT to disassemble simple PowerPC binaries (e.g., Hello World) with basic symbolic operands. --- bolt/lib/Target/PowerPC/CMakeLists.txt | 1 + bolt/lib/Target/PowerPC/PPCMCSymbolizer.cpp | 45 +++++++++++++++++++++ bolt/lib/Target/PowerPC/PPCMCSymbolizer.h | 42 +++++++++++++++++++ clang/lib/AST/ExprClassification.cpp | 7 ++++ 4 files changed, 95 insertions(+) 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/CMakeLists.txt b/bolt/lib/Target/PowerPC/CMakeLists.txt index eebb8120a1644..9b4f81b53bef8 100644 --- a/bolt/lib/Target/PowerPC/CMakeLists.txt +++ b/bolt/lib/Target/PowerPC/CMakeLists.txt @@ -17,6 +17,7 @@ endif() add_llvm_library(LLVMBOLTTargetPowerPC PPCMCPlusBuilder.cpp + PPCMCSymbolizer.cpp NO_EXPORT DISABLE_LLVM_LINK_LLVM_DYLIB diff --git a/bolt/lib/Target/PowerPC/PPCMCSymbolizer.cpp b/bolt/lib/Target/PowerPC/PPCMCSymbolizer.cpp new file mode 100644 index 0000000000000..bb1a583e50958 --- /dev/null +++ b/bolt/lib/Target/PowerPC/PPCMCSymbolizer.cpp @@ -0,0 +1,45 @@ +//===- bolt/Target/PPC/PPCMCSymbolizer.cpp ----------------------*- C++ -*-===// +// +// Minimal PowerPC Symbolizer for BOLT "Hello World" Programs +// +//===----------------------------------------------------------------------===// + +#include "PPCMCSymbolizer.h" +#include "bolt/Core/BinaryFunction.h" +#include "bolt/Core/Relocation.h" +#include "llvm/MC/MCInst.h" + +using namespace llvm; +using namespace bolt; + +PPCMCSymbolizer::~PPCMCSymbolizer() = default; + +bool PPCMCSymbolizer::tryAddingSymbolicOperand( + MCInst &Inst, raw_ostream &CStream, int64_t Value, uint64_t Address, + bool IsBranch, uint64_t Offset, uint64_t OpSize, uint64_t InstSize) { + // 1) Normalize to function-relative offset + BinaryContext &BC = Function.getBinaryContext(); + MCContext *Ctx = BC.Ctx.get(); + const uint64_t InstOffset = Address - Function.getAddress(); + + // 2) Find relocation at "instruction start + immediate offset" + const Relocation *Rel = Function.getRelocationAt(InstOffset + Offset); + if (!Rel) + return false; + + // 3) Build MCExpr = Symbol [+ Addend] and attach as a real operand + const MCSymbol *Sym = Rel->Symbol; // prefer the pointer, not a name string + const MCExpr *Expr = MCSymbolRefExpr::create(Sym, *Ctx); + if (Rel->Addend) + Expr = MCBinaryExpr::createAdd( + Expr, MCConstantExpr::create(Rel->Addend, *Ctx), *Ctx); + + Inst.addOperand(MCOperand::createExpr(Expr)); + return true; +} + +void PPCMCSymbolizer::tryAddingPcLoadReferenceComment(raw_ostream &CStream, + int64_t Value, + uint64_t Address) { + // For "Hello World": no special PC-relative loads, leave empty for now +} \ No newline at end of file diff --git a/bolt/lib/Target/PowerPC/PPCMCSymbolizer.h b/bolt/lib/Target/PowerPC/PPCMCSymbolizer.h new file mode 100644 index 0000000000000..8433c8d0574b5 --- /dev/null +++ b/bolt/lib/Target/PowerPC/PPCMCSymbolizer.h @@ -0,0 +1,42 @@ +//===- bolt/Target/PPC/PPCMCSymbolizer.h ------------------------*- C++ -*-===// +// +// Minimal PowerPC Symbolizer for BOLT "Hello World" Programs +// +//===----------------------------------------------------------------------===// + +#ifndef BOLT_TARGET_PPC_PPCMCSYMBOLIZER_H +#define BOLT_TARGET_PPC_PPCMCSYMBOLIZER_H + +#include "bolt/Core/BinaryFunction.h" +#include "llvm/MC/MCDisassembler/MCSymbolizer.h" + +namespace llvm { +namespace bolt { + +class PPCMCSymbolizer : public MCSymbolizer { +protected: + BinaryFunction &Function; + +public: + PPCMCSymbolizer(BinaryFunction &Function) + : MCSymbolizer(*Function.getBinaryContext().Ctx, nullptr), + Function(Function) {} + + PPCMCSymbolizer(const PPCMCSymbolizer &) = delete; + PPCMCSymbolizer &operator=(const PPCMCSymbolizer &) = delete; + virtual ~PPCMCSymbolizer(); + + /// Minimal: Try to add a symbolic operand if there is a matching relocation + bool tryAddingSymbolicOperand(MCInst &Inst, raw_ostream &CStream, + int64_t Value, uint64_t Address, bool IsBranch, + uint64_t Offset, uint64_t OpSize, + uint64_t InstSize) override; + + void tryAddingPcLoadReferenceComment(raw_ostream &CStream, int64_t Value, + uint64_t Address) override; +}; + +} // namespace bolt +} // namespace llvm + +#endif diff --git a/clang/lib/AST/ExprClassification.cpp b/clang/lib/AST/ExprClassification.cpp index ad66335138a42..aeacd0dc765ef 100644 --- a/clang/lib/AST/ExprClassification.cpp +++ b/clang/lib/AST/ExprClassification.cpp @@ -601,6 +601,13 @@ static Cl::Kinds ClassifyMemberExpr(ASTContext &Ctx, const MemberExpr *E) { static Cl::Kinds ClassifyBinaryOp(ASTContext &Ctx, const BinaryOperator *E) { assert(Ctx.getLangOpts().CPlusPlus && "This is only relevant for C++."); + + // For binary operators which are unknown due to type dependence, the + // convention is to classify them as a prvalue. This does not matter much, but + // it needs to agree with how they are created. + if (E->getType() == Ctx.DependentTy) + return Cl::CL_PRValue; + // C++ [expr.ass]p1: All [...] return an lvalue referring to the left operand. // Except we override this for writes to ObjC properties. if (E->isAssignmentOp()) _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits