From 3308180ffec0a04dda478a8db0a4e2281cb82bb3 Mon Sep 17 00:00:00 2001 From: Andy Ragusa Date: Thu, 18 Jan 2024 15:33:10 -0800 Subject: [PATCH] blah --- examples/CMakeLists.txt | 68 +-------- examples/LegacyPassManager/CMakeLists.txt | 67 +++++++++ .../HelloWorld/HelloWorld.cpp | 2 +- .../AnalysisPlugin/AnalysisPlugin.cpp | 129 ++++++++++++++++++ .../PassManager/AnalysisPlugin/CMakeLists.txt | 72 ++++++++++ examples/PassManager/CMakeLists.txt | 4 + examples/PassManager/input/compile.sh | 47 +++++++ examples/PassManager/input/run_opt.sh | 4 + 8 files changed, 329 insertions(+), 64 deletions(-) create mode 100644 examples/LegacyPassManager/CMakeLists.txt rename examples/{ => LegacyPassManager}/HelloWorld/HelloWorld.cpp (96%) create mode 100644 examples/PassManager/AnalysisPlugin/AnalysisPlugin.cpp create mode 100644 examples/PassManager/AnalysisPlugin/CMakeLists.txt create mode 100644 examples/PassManager/CMakeLists.txt create mode 100755 examples/PassManager/input/compile.sh create mode 100755 examples/PassManager/input/run_opt.sh diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index dea625e7cb..369f3cc1bd 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,67 +1,9 @@ # Copyright (C) 2021 Cisco Systems, Inc. and/or its affiliates. All rights reserved. -# -# The hello object library -# -add_library(hello_obj OBJECT) -target_sources(hello_obj - PRIVATE - HelloWorld/HelloWorld.cpp -) -target_include_directories(hello_obj - PRIVATE - ../libclambcc # HACK: For Common/clambc.h - ${LLVM_INCLUDE_DIRS} -) +#'PassManager' is using the 'new' passmanager. This was added +#for the upgrade to llvm 16, although the 'new' pass manager +#has been around a while. -set_target_properties(hello_obj PROPERTIES COMPILE_FLAGS "${WARNCXXFLAGS}") - -# -# For testing -# -#target_compile_definitions(clambc_obj -DLOG_BEFORE_AFTER=1) - -# -# The hello shared library. -# -add_library( hello SHARED ) -target_link_libraries( hello - PUBLIC - hello_obj ) -set_target_properties( hello PROPERTIES - VERSION ${LIBCLAMBC_VERSION} - SOVERSION ${LIBCLAMBC_SOVERSION} ) - -target_link_directories(hello_obj PRIVATE ${LLVM_LIBRARY_DIRS}) -target_link_libraries(hello_obj PUBLIC ${LLVM_LIBS}) - -if(WIN32) - install(TARGETS hello DESTINATION .) - - # Also install shared library (DLL) dependencies - install(CODE [[ - file(GET_RUNTIME_DEPENDENCIES - LIBRARIES - $ - RESOLVED_DEPENDENCIES_VAR _r_deps - UNRESOLVED_DEPENDENCIES_VAR _u_deps - DIRECTORIES - ${LLVM_LIBRARY_DIRS} - ) - foreach(_file ${_r_deps}) - string(TOLOWER ${_file} _file_lower) - if(NOT ${_file_lower} MATCHES "c:[\\/]windows[\\/]system32.*") - file(INSTALL - DESTINATION "${CMAKE_INSTALL_PREFIX}" - TYPE SHARED_LIBRARY - FOLLOW_SYMLINK_CHAIN - FILES "${_file}" - ) - endif() - endforeach() - #message("UNRESOLVED_DEPENDENCIES_VAR: ${_u_deps}") - ]]) -else() - install(TARGETS hello DESTINATION ${CMAKE_INSTALL_LIBDIR}) -endif() +add_subdirectory(LegacyPassManager) +add_subdirectory(PassManager) diff --git a/examples/LegacyPassManager/CMakeLists.txt b/examples/LegacyPassManager/CMakeLists.txt new file mode 100644 index 0000000000..dea625e7cb --- /dev/null +++ b/examples/LegacyPassManager/CMakeLists.txt @@ -0,0 +1,67 @@ +# Copyright (C) 2021 Cisco Systems, Inc. and/or its affiliates. All rights reserved. + +# +# The hello object library +# +add_library(hello_obj OBJECT) +target_sources(hello_obj + PRIVATE + HelloWorld/HelloWorld.cpp +) + +target_include_directories(hello_obj + PRIVATE + ../libclambcc # HACK: For Common/clambc.h + ${LLVM_INCLUDE_DIRS} +) + +set_target_properties(hello_obj PROPERTIES COMPILE_FLAGS "${WARNCXXFLAGS}") + +# +# For testing +# +#target_compile_definitions(clambc_obj -DLOG_BEFORE_AFTER=1) + +# +# The hello shared library. +# +add_library( hello SHARED ) +target_link_libraries( hello + PUBLIC + hello_obj ) +set_target_properties( hello PROPERTIES + VERSION ${LIBCLAMBC_VERSION} + SOVERSION ${LIBCLAMBC_SOVERSION} ) + +target_link_directories(hello_obj PRIVATE ${LLVM_LIBRARY_DIRS}) +target_link_libraries(hello_obj PUBLIC ${LLVM_LIBS}) + +if(WIN32) + install(TARGETS hello DESTINATION .) + + # Also install shared library (DLL) dependencies + install(CODE [[ + file(GET_RUNTIME_DEPENDENCIES + LIBRARIES + $ + RESOLVED_DEPENDENCIES_VAR _r_deps + UNRESOLVED_DEPENDENCIES_VAR _u_deps + DIRECTORIES + ${LLVM_LIBRARY_DIRS} + ) + foreach(_file ${_r_deps}) + string(TOLOWER ${_file} _file_lower) + if(NOT ${_file_lower} MATCHES "c:[\\/]windows[\\/]system32.*") + file(INSTALL + DESTINATION "${CMAKE_INSTALL_PREFIX}" + TYPE SHARED_LIBRARY + FOLLOW_SYMLINK_CHAIN + FILES "${_file}" + ) + endif() + endforeach() + #message("UNRESOLVED_DEPENDENCIES_VAR: ${_u_deps}") + ]]) +else() + install(TARGETS hello DESTINATION ${CMAKE_INSTALL_LIBDIR}) +endif() diff --git a/examples/HelloWorld/HelloWorld.cpp b/examples/LegacyPassManager/HelloWorld/HelloWorld.cpp similarity index 96% rename from examples/HelloWorld/HelloWorld.cpp rename to examples/LegacyPassManager/HelloWorld/HelloWorld.cpp index cdd120b5e7..f8680f20d6 100644 --- a/examples/HelloWorld/HelloWorld.cpp +++ b/examples/LegacyPassManager/HelloWorld/HelloWorld.cpp @@ -10,7 +10,7 @@ #include "llvm/IR/LegacyPassManager.h" #include "llvm/Transforms/IPO/PassManagerBuilder.h" -#include "Common/clambc.h" +//#include "Common/clambc.h" using namespace llvm; namespace { diff --git a/examples/PassManager/AnalysisPlugin/AnalysisPlugin.cpp b/examples/PassManager/AnalysisPlugin/AnalysisPlugin.cpp new file mode 100644 index 0000000000..450236f98a --- /dev/null +++ b/examples/PassManager/AnalysisPlugin/AnalysisPlugin.cpp @@ -0,0 +1,129 @@ +/* + * Compile LLVM bytecode to ClamAV bytecode. + * + * Copyright (C) 2020-2023 Sourcefire, Inc. + * + * Authors: Andy Ragusa + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +//#include "Common/clambc.h" +//#include "Common/ClamBCUtilities.h" + +#include +#include +#include + +#include +#include +#include + +#include + +using namespace llvm; +using namespace std; + +/* Modeled after CallGraphAnalysis */ + +namespace +{ + + class AnalysisResult { + public: + AnalysisResult(){ + llvm::errs() << "<" << __FUNCTION__ << "::" << __LINE__ << ">" << "\n"; + } + + }; + + class ExampleAnalysis : public AnalysisInfoMixin + { + + public: + + friend AnalysisInfoMixin ; + static AnalysisKey Key; + + + ExampleAnalysis(){ + } + + typedef AnalysisResult Result; + + AnalysisResult run(llvm::Module & F, llvm::ModuleAnalysisManager & fam){ + + llvm::errs() << "<" << "Analysis::" << __LINE__ << ">" << "\n"; + return AnalysisResult(); + + } + + }; + + AnalysisKey ExampleAnalysis::Key; + + struct ExamplePass : public PassInfoMixin + { + protected: + Module *pMod = nullptr; + bool bChanged = false; + + public: + + virtual ~ExamplePass() {} + + PreservedAnalyses run(Module & m, ModuleAnalysisManager & MAM) + { + pMod = &m; + llvm::errs() << "<" << __FUNCTION__ << "::" << __LINE__ << ">" << "Transform Pass" << "\n"; + + MAM.getResult(m); + + llvm::errs() << "<" << __FUNCTION__ << "::" << __LINE__ << ">" << "Transform Pass (leaving)" << "\n"; + + return PreservedAnalyses::all(); + } + }; // end of struct ExamplePass + +} // end of anonymous namespace + +// This part is the new way of registering your pass +extern "C" ::llvm::PassPluginLibraryInfo LLVM_ATTRIBUTE_WEAK +llvmGetPassPluginInfo() { + return { + LLVM_PLUGIN_API_VERSION, "ExamplePass", "v0.1", + [](PassBuilder &PB) { + PB.registerPipelineParsingCallback( + [](StringRef Name, ModulePassManager &FPM, + ArrayRef) { + if(Name == "example-pass-with-analysis"){ + FPM.addPass(ExamplePass()); + return true; + } + return false; + } + ); + + PB.registerAnalysisRegistrationCallback( + [](ModuleAnalysisManager &mam) { + mam.registerPass([] () { return ExampleAnalysis(); } ); + } + ); + } + }; +} + + + diff --git a/examples/PassManager/AnalysisPlugin/CMakeLists.txt b/examples/PassManager/AnalysisPlugin/CMakeLists.txt new file mode 100644 index 0000000000..9a8ad5d6db --- /dev/null +++ b/examples/PassManager/AnalysisPlugin/CMakeLists.txt @@ -0,0 +1,72 @@ +# Copyright (C) 2021 Cisco Systems, Inc. and/or its affiliates. All rights reserved. + +# +# The analysisplugin object library +# +add_library(analysisplugin_obj OBJECT) +target_sources(analysisplugin_obj + PRIVATE + AnalysisPlugin.cpp +) + +target_include_directories(analysisplugin_obj + PRIVATE + ${CMAKE_BINARY_DIR} # For clambc-version.h (generated file) + . # For Common/clambc.h + .. # For clambc.h #TODO: change all passes to use "Common" and then delete this line. + ${LLVM_INCLUDE_DIRS} +) + +set_target_properties(analysisplugin_obj PROPERTIES COMPILE_FLAGS "${WARNCXXFLAGS}") + +# +# For testing +# +#target_compile_definitions(analysisplugin_obj -DLOG_BEFORE_AFTER=1) + +# +# The analysisplugin shared library. +# +add_library( analysisplugin SHARED ) +target_link_libraries( analysisplugin + PUBLIC + analysisplugin_obj ) +set_target_properties( analysisplugin PROPERTIES + VERSION ${LIBCLAMBC_VERSION} + SOVERSION ${LIBCLAMBC_SOVERSION} ) + +target_link_directories(analysisplugin PRIVATE ${LLVM_LIBRARY_DIRS}) +target_link_libraries(analysisplugin PUBLIC ${LLVM_LIBS}) + +if(WIN32) + install(TARGETS analysisplugin DESTINATION .) + + # Also install shared library (DLL) dependencies + install(CODE [[ + file(GET_RUNTIME_DEPENDENCIES + LIBRARIES + $ + RESOLVED_DEPENDENCIES_VAR _r_deps + UNRESOLVED_DEPENDENCIES_VAR _u_deps + DIRECTORIES + ${LLVM_LIBRARY_DIRS} + ) + foreach(_file ${_r_deps}) + string(TOLOWER ${_file} _file_lower) + if(NOT ${_file_lower} MATCHES "c:[\\/]windows[\\/]system32.*") + file(INSTALL + DESTINATION "${CMAKE_INSTALL_PREFIX}" + TYPE SHARED_LIBRARY + FOLLOW_SYMLINK_CHAIN + FILES "${_file}" + ) + endif() + endforeach() + #message("UNRESOLVED_DEPENDENCIES_VAR: ${_u_deps}") + ]]) +else() + install(TARGETS analysisplugin DESTINATION ${CMAKE_INSTALL_LIBDIR}) +endif() + + + diff --git a/examples/PassManager/CMakeLists.txt b/examples/PassManager/CMakeLists.txt new file mode 100644 index 0000000000..ec133b4ea2 --- /dev/null +++ b/examples/PassManager/CMakeLists.txt @@ -0,0 +1,4 @@ +# Copyright (C) 2021 Cisco Systems, Inc. and/or its affiliates. All rights reserved. + + +add_subdirectory(AnalysisPlugin) diff --git a/examples/PassManager/input/compile.sh b/examples/PassManager/input/compile.sh new file mode 100755 index 0000000000..ac8d245db5 --- /dev/null +++ b/examples/PassManager/input/compile.sh @@ -0,0 +1,47 @@ +#!/bin/bash + + +SOURCE_FILE=analysis_test.c + +echo "#include " >> $SOURCE_FILE +echo " " >> $SOURCE_FILE +echo "int func2(int i){ " >> $SOURCE_FILE +echo " return i/2; " >> $SOURCE_FILE +echo "} " >> $SOURCE_FILE +echo " " >> $SOURCE_FILE +echo "int func(int idx){ " >> $SOURCE_FILE +echo " int tmp; " >> $SOURCE_FILE +echo " " >> $SOURCE_FILE +echo " if (idx > 1){ " >> $SOURCE_FILE +echo " tmp = func2(11); " >> $SOURCE_FILE +echo " } else { " >> $SOURCE_FILE +echo " tmp = func(idx-1); " >> $SOURCE_FILE +echo " } " >> $SOURCE_FILE +echo " " >> $SOURCE_FILE +echo " if (0 == tmp){ " >> $SOURCE_FILE +echo " return 0; " >> $SOURCE_FILE +echo " } " >> $SOURCE_FILE +echo " return idx-1; " >> $SOURCE_FILE +echo "} " >> $SOURCE_FILE +echo " " >> $SOURCE_FILE +echo "int main(int argc, char ** argv){ " >> $SOURCE_FILE +echo " " >> $SOURCE_FILE +echo " if (argc){ " >> $SOURCE_FILE +echo " func(argc); " >> $SOURCE_FILE +echo " } " >> $SOURCE_FILE +echo " " >> $SOURCE_FILE +echo " " >> $SOURCE_FILE +echo " return 0; " >> $SOURCE_FILE +echo "} " >> $SOURCE_FILE +echo " " >> $SOURCE_FILE + + +clang-16 \ + -S \ + -fno-discard-value-names \ + --language=c \ + -emit-llvm \ + -Werror=unused-command-line-argument \ + -Xclang \ + -disable-O0-optnone \ + $SOURCE_FILE diff --git a/examples/PassManager/input/run_opt.sh b/examples/PassManager/input/run_opt.sh new file mode 100755 index 0000000000..77544e4eb5 --- /dev/null +++ b/examples/PassManager/input/run_opt.sh @@ -0,0 +1,4 @@ +#!/bin/bash + +opt-16 -load-pass-plugin examples/NewPassManager/AnalysisPlugin/libanalysisplugin.so -passes=example-pass-with-analysis analysis_test.ll -o analysis_test.t.ll +