Skip to content

Commit

Permalink
Need to verify testing before I squash the commits
Browse files Browse the repository at this point in the history
  • Loading branch information
ragusaa committed Jan 10, 2024
1 parent 6d8e868 commit b1b029e
Show file tree
Hide file tree
Showing 5 changed files with 239 additions and 163 deletions.
4 changes: 2 additions & 2 deletions clambcc/clambc-compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ def createInputSourceFile(clangLLVM: ClangLLVM, name: str, args: list, options:
# , 'verify'
, 'clambc-extend-phis-to-64-bit'
, 'verify'
, 'clambc-convert-memsets-to-32Bit'
, 'clambc-convert-intrinsics-to-32Bit'
, 'verify'
, 'globalopt'
, 'clambc-prepare-geps-for-writer'
Expand Down Expand Up @@ -689,7 +689,7 @@ def createInputSourceFile(clangLLVM: ClangLLVM, name: str, args: list, options:
, f"--load-pass-plugin {SHARED_OBJ_DIR}/libclambcoutlineendiannesscalls.so"
, f"--load-pass-plugin {SHARED_OBJ_DIR}/libclambcchangemallocargsize.so"
, f"--load-pass-plugin {SHARED_OBJ_DIR}/libclambcextendphisto64bit.so"
, f"--load-pass-plugin {SHARED_OBJ_DIR}/libclambcconvertmemsetsto32bit.so"
, f"--load-pass-plugin {SHARED_OBJ_DIR}/libclambcconvertintrinsicsto32bit.so"
, f"--load-pass-plugin {SHARED_OBJ_DIR}/libclambcpreparegepsforwriter.so"
, f"--load-pass-plugin {SHARED_OBJ_DIR}/libclambcanalyzer.so"
, f"--load-pass-plugin {SHARED_OBJ_DIR}/libclambcregalloc.so"
Expand Down
2 changes: 1 addition & 1 deletion libclambcc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ add_subdirectory(ClamBCChangeMallocArgSize)
add_subdirectory(ClamBCExtendPHIsTo64Bit)
add_subdirectory(ClamBCRebuild)
add_subdirectory(ClamBCRegAlloc)
add_subdirectory(ClamBCConvertMemsetsTo32Bit)
add_subdirectory(ClamBCConvertIntrinsicsTo32Bit)
add_subdirectory(ClamBCPrepareGEPsForWriter)
add_subdirectory(ClamBCRemoveICMPSLE)
add_subdirectory(ClamBCRemoveUSUB)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,51 +1,51 @@
# Copyright (C) 2021 Cisco Systems, Inc. and/or its affiliates. All rights reserved.

#
# The clambcconvertmemsetsto32bit object library
# The clambcconvertintrinsicsto32bit object library
#
add_library(clambcconvertmemsetsto32bit_obj OBJECT)
target_sources(clambcconvertmemsetsto32bit_obj
add_library(clambcconvertintrinsicsto32bit_obj OBJECT)
target_sources(clambcconvertintrinsicsto32bit_obj
PRIVATE
ClamBCConvertMemsetsTo32Bit.cpp
ClamBCConvertIntrinsicsTo32Bit.cpp
)

target_include_directories(clambcconvertmemsetsto32bit_obj
target_include_directories(clambcconvertintrinsicsto32bit_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(clambcconvertmemsetsto32bit_obj PROPERTIES COMPILE_FLAGS "${WARNCXXFLAGS}")
set_target_properties(clambcconvertintrinsicsto32bit_obj PROPERTIES COMPILE_FLAGS "${WARNCXXFLAGS}")

#
# For testing
#
#target_compile_definitions(clambcconvertmemsetsto32bit_obj -DLOG_BEFORE_AFTER=1)
#target_compile_definitions(clambcconvertintrinsicsto32bit_obj -DLOG_BEFORE_AFTER=1)

#
# The clambcconvertmemsetsto32bit shared library.
# The clambcconvertintrinsicsto32bit shared library.
#
add_library( clambcconvertmemsetsto32bit SHARED )
target_link_libraries( clambcconvertmemsetsto32bit
add_library( clambcconvertintrinsicsto32bit SHARED )
target_link_libraries( clambcconvertintrinsicsto32bit
PUBLIC
clambcconvertmemsetsto32bit_obj )
set_target_properties( clambcconvertmemsetsto32bit PROPERTIES
clambcconvertintrinsicsto32bit_obj )
set_target_properties( clambcconvertintrinsicsto32bit PROPERTIES
VERSION ${LIBCLAMBC_VERSION}
SOVERSION ${LIBCLAMBC_SOVERSION} )

target_link_directories(clambcconvertmemsetsto32bit PRIVATE ${LLVM_LIBRARY_DIRS})
target_link_libraries(clambcconvertmemsetsto32bit PUBLIC ${LLVM_LIBS})
target_link_directories(clambcconvertintrinsicsto32bit PRIVATE ${LLVM_LIBRARY_DIRS})
target_link_libraries(clambcconvertintrinsicsto32bit PUBLIC ${LLVM_LIBS})

if(WIN32)
install(TARGETS clambcconvertmemsetsto32bit DESTINATION .)
install(TARGETS clambcconvertintrinsicsto32bit DESTINATION .)

# Also install shared library (DLL) dependencies
install(CODE [[
file(GET_RUNTIME_DEPENDENCIES
LIBRARIES
$<TARGET_FILE:clambcconvertmemsetsto32bit>
$<TARGET_FILE:clambcconvertintrinsicsto32bit>
RESOLVED_DEPENDENCIES_VAR _r_deps
UNRESOLVED_DEPENDENCIES_VAR _u_deps
DIRECTORIES
Expand All @@ -65,7 +65,7 @@ if(WIN32)
#message("UNRESOLVED_DEPENDENCIES_VAR: ${_u_deps}")
]])
else()
install(TARGETS clambcconvertmemsetsto32bit DESTINATION ${CMAKE_INSTALL_LIBDIR})
install(TARGETS clambcconvertintrinsicsto32bit DESTINATION ${CMAKE_INSTALL_LIBDIR})
endif()


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@

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

#include <llvm/IR/DerivedTypes.h>
#include <llvm/IR/PassManager.h>
#include <llvm/Passes/PassBuilder.h>
#include <llvm/Passes/PassPlugin.h>

#include <llvm/IR/Dominators.h>
#include <llvm/IR/Constants.h>
#include <llvm/Transforms/IPO/PassManagerBuilder.h>

#include "Common/clambc.h"

#include <vector>

using namespace llvm;

namespace ClamBCConvertIntrinsicsTo32Bit {

class ClamBCConvertIntrinsicsTo32Bit : public PassInfoMixin<ClamBCConvertIntrinsicsTo32Bit >
{

public:
static char ID;

ClamBCConvertIntrinsicsTo32Bit() {}

virtual ~ClamBCConvertIntrinsicsTo32Bit() {}

PreservedAnalyses run(Module & mod, ModuleAnalysisManager & MAM)
{
bChanged = false;
pMod = &mod;

initializeReplacements();

for (auto i = pMod->begin(), e = pMod->end(); i != e; i++) {
Function* pFunc = llvm::cast<Function>(i);
processFunction(pFunc);
}

for (size_t i = 0; i < delLst.size(); i++) {
delLst[i]->eraseFromParent();
}

if (bChanged){
return PreservedAnalyses::none();
}

return PreservedAnalyses::all();
}

protected:
Module* pMod = nullptr;
bool bChanged = false;
std::vector<CallInst*> delLst;

typedef struct {
llvm::Function * oldFunc;
llvm::FunctionCallee newFunc;
const size_t paramIdx;
} Replacement;
std::vector< Replacement> replacements;

llvm::FunctionType* getMemset32Type()
{
LLVMContext& c = pMod->getContext();
return FunctionType::get(Type::getVoidTy(c),
{Type::getInt8PtrTy(c), Type::getInt8Ty(c), Type::getInt32Ty(c), Type::getInt1Ty(c)},
false);
}

llvm::FunctionType* getMemcpy32Type()
{
LLVMContext& c = pMod->getContext();
return FunctionType::get(Type::getVoidTy(c),
{Type::getInt8PtrTy(c), Type::getInt8PtrTy(c), Type::getInt32Ty(c), Type::getInt1Ty(c)},
false);
}

llvm::FunctionType* getMemmove32Type()
{
LLVMContext& c = pMod->getContext();
return FunctionType::get(Type::getVoidTy(c),
{Type::getInt8PtrTy(c), Type::getInt8PtrTy(c), Type::getInt32Ty(c), Type::getInt1Ty(c)},
false);
}

void initializeReplacements(){
/*There are different calls when you use the -no-opaque flags.*/

/*memsets*/
FunctionType * ft = getMemset32Type();
Function * pFunc = pMod->getFunction( "llvm.memset.p0i8.i64");
if (pFunc){
FunctionCallee rep = pMod->getOrInsertFunction("llvm.memset.p0i8.i32", ft);
replacements.push_back( {pFunc, rep, 2} );
}
pFunc = pMod->getFunction( "llvm.memset.p0.i64");
if (pFunc){
FunctionCallee rep = pMod->getOrInsertFunction("llvm.memset.p0.i32", ft);
replacements.push_back( {pFunc, rep, 2});
}

/*memcpys*/
ft = getMemcpy32Type();
pFunc = pMod->getFunction("llvm.memcpy.p0i8.p0i8.i64");
if (pFunc){
FunctionCallee rep = pMod->getOrInsertFunction("llvm.memcpy.p0i8.p0i8.i32", ft);
replacements.push_back( {pFunc, rep, 2});
}
pFunc = pMod->getFunction("llvm.memcpy.p0.p0.i64");
if (pFunc){
FunctionCallee rep = pMod->getOrInsertFunction("llvm.memcpy.p0.p0.i32", ft);
replacements.push_back( {pFunc, rep, 2});
}

/*memmoves*/
ft = getMemmove32Type();
pFunc = pMod->getFunction("llvm.memmove.p0.p0.i64");
if (pFunc){
FunctionCallee rep = pMod->getOrInsertFunction("llvm.memmove.p0.p0.i32", ft);
replacements.push_back( {pFunc, rep, 2});
}
pFunc = pMod->getFunction("llvm.memmove.p0i8.p0i8.i64");
if (pFunc){
FunctionCallee rep = pMod->getOrInsertFunction("llvm.memmove.p0i8.p0i8.i32", ft);
replacements.push_back( {pFunc, rep, 2});
}

}

void processFunction(Function* pFunc)
{
for (auto i = pFunc->begin(), e = pFunc->end(); i != e; i++) {
BasicBlock* pBB = llvm::cast<BasicBlock>(i);
processBasicBlock(pBB);
}
}

void processBasicBlock(BasicBlock* pBB)
{
for (auto i = pBB->begin(), e = pBB->end(); i != e; i++) {
if (CallInst* pci = llvm::dyn_cast<CallInst>(i)) {
Function * f = pci->getCalledFunction();
if (nullptr != f) {
for (size_t i = 0; i < replacements.size(); i++){
if (replacements[i].oldFunc == f) {
convertCall(pci, replacements[i]);
}
}
}
}
}
}

void convertCall(CallInst* pci, const Replacement & r)
{
std::vector<Value*> args;
Type* i32Ty = Type::getInt32Ty(pMod->getContext());

for (size_t i = 0; i < pci->arg_size(); i++)
{
Value* pv = pci->getArgOperand(i);
if (r.paramIdx == i) {
if (ConstantInt* ci = llvm::dyn_cast<ConstantInt>(pv)) {
pv = ConstantInt::get(i32Ty, ci->getValue().getLimitedValue());
} else {
pv = CastInst::CreateTruncOrBitCast(pv, i32Ty, "ClamBCConvertIntrinsicsTo32Bit_trunc_", pci);
}

pci->setArgOperand(i, pv);
}

// args.push_back(pv);
}


//FunctionCallee f = pMod->getOrInsertFunction(newName, );
pci->setCalledFunction(r.newFunc);


// CallInst * pNew = CallInst::Create(f, args, "", pci);
// pNew->setAttributes(pci->getAttributes());

// delLst.push_back(pci);
}


};

} // 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, "ClamBCConvertIntrinsicsTo32Bit", "v0.1",
[](PassBuilder &PB) {
PB.registerPipelineParsingCallback(
[](StringRef Name, ModulePassManager &FPM,
ArrayRef<PassBuilder::PipelineElement>) {
if(Name == "clambc-convert-intrinsics-to-32Bit"){
FPM.addPass(ClamBCConvertIntrinsicsTo32Bit::ClamBCConvertIntrinsicsTo32Bit());
return true;
}
return false;
}
);
}
};
}


Loading

0 comments on commit b1b029e

Please sign in to comment.