Skip to content

Commit

Permalink
ClamBCPreserveABIs. (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
ragusaa authored Aug 15, 2023
1 parent 876e804 commit 21b10db
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 11 deletions.
2 changes: 1 addition & 1 deletion libclambcc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


add_subdirectory(ClamBCRemoveUndefs)
#add_subdirectory(ClamBCPreserveABIs)
add_subdirectory(ClamBCPreserveABIs)
#add_subdirectory(ClamBCAnalyzer)
add_subdirectory(Common)

72 changes: 72 additions & 0 deletions libclambcc/ClamBCPreserveABIs/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Copyright (C) 2021 Cisco Systems, Inc. and/or its affiliates. All rights reserved.

#
# The clambcpreserveabis object library
#
add_library(clambcpreserveabis_obj OBJECT)
target_sources(clambcpreserveabis_obj
PRIVATE
ClamBCPreserveABIs.cpp
)

target_include_directories(clambcpreserveabis_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(clambcpreserveabis_obj PROPERTIES COMPILE_FLAGS "${WARNCXXFLAGS}")

#
# For testing
#
#target_compile_definitions(clambcpreserveabis_obj -DLOG_BEFORE_AFTER=1)

#
# The clambcpreserveabis shared library.
#
add_library( clambcpreserveabis SHARED )
target_link_libraries( clambcpreserveabis
PUBLIC
clambcpreserveabis_obj )
set_target_properties( clambcpreserveabis PROPERTIES
VERSION ${LIBCLAMBC_VERSION}
SOVERSION ${LIBCLAMBC_SOVERSION} )

target_link_directories(clambcpreserveabis PRIVATE ${LLVM_LIBRARY_DIRS})
target_link_libraries(clambcpreserveabis PUBLIC ${LLVM_LIBS})

if(WIN32)
install(TARGETS clambcpreserveabis DESTINATION .)

# Also install shared library (DLL) dependencies
install(CODE [[
file(GET_RUNTIME_DEPENDENCIES
LIBRARIES
$<TARGET_FILE:clambcpreserveabis>
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 clambcpreserveabis DESTINATION ${CMAKE_INSTALL_LIBDIR})
endif()



75 changes: 65 additions & 10 deletions libclambcc/ClamBCPreserveABIs/ClamBCPreserveABIs.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@

#include "Common/clambc.h"
#include "Common/ClamBCUtilities.h"

#include <llvm/Pass.h>
#include "llvm/IR/Module.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Instructions.h"
#include "llvm/Support/raw_ostream.h"
#include <llvm/IR/Module.h>
#include <llvm/IR/Instructions.h>
#include <llvm/Support/raw_ostream.h>

#include <llvm/IR/Dominators.h>

#include "llvm/IR/LegacyPassManager.h"
#include "llvm/Transforms/IPO/PassManagerBuilder.h"
//#include <llvm/IR/LegacyPassManager.h>
#include <llvm/Transforms/IPO/PassManagerBuilder.h>
#include <llvm/Passes/PassPlugin.h>
#include <llvm/Passes/PassBuilder.h>

#include "Common/clambc.h"
#include "Common/ClamBCUtilities.h"

#include <sstream>
#include <string>



using namespace llvm;

namespace
Expand All @@ -32,7 +37,7 @@ namespace
* to fake functions. If it does find it (the second time), it removes those
* calls.
*/
class ClamBCPreserveABIs : public ModulePass
class ClamBCPreserveABIs : public PassInfoMixin<ClamBCPreserveABIs>
{
protected:
llvm::Module *pMod = nullptr;
Expand All @@ -46,9 +51,17 @@ class ClamBCPreserveABIs : public ModulePass
return;
}
FunctionType *pFunctionType = llvm::dyn_cast<FunctionType>(pFunc->getType());
#if 0
std::string newname = pFunc->getName();
#else
std::string newname( pFunc->getName());
#endif
newname += "_fake";
#if 0
pFunctionType = llvm::cast<FunctionType>(llvm::cast<PointerType>(pFunc->getType())->getElementType());
#else
pFunctionType = pFunc->getFunctionType();
#endif
Function *fakeFunction = Function::Create(pFunctionType, Function::ExternalLinkage, newname, pFunc->getParent());
fakeFunctions.push_back(fakeFunction);
std::vector<Value *> args;
Expand Down Expand Up @@ -127,18 +140,28 @@ class ClamBCPreserveABIs : public ModulePass
}

public:
#if 0
static char ID;
ClamBCPreserveABIs()
: ModulePass(ID) {}
#endif

virtual ~ClamBCPreserveABIs() {}

#if 0
bool runOnModule(Module &m) override
#else
virtual PreservedAnalyses run(Module & m, ModuleAnalysisManager & MAM)
#endif
{
pMod = &m;

if (removeFakeFunctions()) {
#if 0
return bChanged;
#else
return PreservedAnalyses::none();
#endif
}

for (auto i = pMod->begin(), e = pMod->end(); i != e; i++) {
Expand All @@ -151,19 +174,51 @@ class ClamBCPreserveABIs : public ModulePass
/*Set the linkage type to external so that the optimizer cannot remove the arguments.*/
pFunc->setLinkage(GlobalValue::ExternalLinkage);
}

processFunction(pFunc);
}

writeMetadata();

#if 0
return bChanged;
#else
if (bChanged){
return PreservedAnalyses::none();
}
return PreservedAnalyses::all();
#endif
}
}; // end of struct ClamBCPreserveABIs

} // end of anonymous namespace

#if 0
char ClamBCPreserveABIs::ID = 0;
static RegisterPass<ClamBCPreserveABIs> X("clambc-preserve-abis", "Preserve ABIs",
false /* Only looks at CFG */,
false /* Analysis Pass */);
#else

// This part is the new way of registering your pass
extern "C" ::llvm::PassPluginLibraryInfo LLVM_ATTRIBUTE_WEAK
llvmGetPassPluginInfo() {
return {
LLVM_PLUGIN_API_VERSION, "ClamBCPreserveABIs", "v0.1",
[](PassBuilder &PB) {
PB.registerPipelineParsingCallback(
[](StringRef Name, ModulePassManager &FPM,
ArrayRef<PassBuilder::PipelineElement>) {
if(Name == "clambc-preserve-abis"){
FPM.addPass(ClamBCPreserveABIs());
return true;
}
return false;
}
);
}
};
}



#endif

0 comments on commit 21b10db

Please sign in to comment.