Skip to content

Commit

Permalink
vortex-mno-riscv-attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
shin0403 committed Oct 14, 2023
1 parent 9c1695c commit f47c44c
Show file tree
Hide file tree
Showing 9 changed files with 186 additions and 23 deletions.
18 changes: 9 additions & 9 deletions lib/CL/devices/vortex/pocl-vortex.c
Original file line number Diff line number Diff line change
Expand Up @@ -392,18 +392,18 @@ pocl_vortex_init (unsigned j, cl_device_id device, const char* parameters)
//pocl_setup_extensions_with_version (device);
char extensions[1024];
extensions[0] = 0;
strcat (extensions, "cl_khr_byte_addressable_store"
/* strcat (extensions, "cl_khr_byte_addressable_store"
" cl_khr_global_int32_base_atomics"
" cl_khr_global_int32_extended_atomics"
" cl_khr_local_int32_base_atomics"
" cl_khr_local_int32_extended_atomics");
strcat (extensions, "cl_khr_3d_image_writes"
"cl_khr_spir"
"cl_khr_fp16"
"cl_khr_int64_base_atomics"
"cl_khr_int64_extended_atomics"
"cl_khr_fp64");

" cl_khr_local_int32_extended_atomics");
strcat (extensions, //" cl_khr_3d_image_writes"
//" cl_khr_spir"
//" cl_khr_fp16"
//" cl_khr_int64_base_atomics"
//" cl_khr_int64_extended_atomics"
//" cl_khr_fp64"
); */
device->extensions = strdup (extensions);

return ret;
Expand Down
2 changes: 2 additions & 0 deletions lib/CL/pocl_llvm_wg.cc
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,9 @@ static PassManager &kernel_compiler_passes(cl_device_id device) {

std::vector<std::string> passes;
#ifdef BUILD_VORTEX
passes.push_back("vortex-mno-riscv-attribute");
passes.push_back("vortex-printfs");
//passes.push_back("print-module");
#endif

passes.push_back("inline-kernels");
Expand Down
12 changes: 9 additions & 3 deletions lib/kernel/vortex/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@

include("bitcode_rules")

set(LLVM_TARGET riscv32)
set(LLVM_TARGET riscv32-unknown-unknown-elf)
#set(LLVM_TARGET riscv32)

set(KERNEL_SOURCES ${SOURCES_GENERIC})

Expand All @@ -38,13 +39,18 @@ foreach(FILE printf.c)
list(APPEND KERNEL_SOURCES "vortex/${FILE}")
endforeach()

set(CLANG_FLAGS "-ffreestanding" "-target" "${LLVM_TARGET}" "-emit-llvm" "-D_CL_DISABLE_HALF")
# TODO : we need to add some flag like -mno-riscv-attribute in GNU compiler, but I cannot find right flag similar flag in clang.
# the kernel function contain useless RISCV extensions in the function argument which are not need for the vortex.

set(CLANG_FLAGS "-ffreestanding" "-v" "-Xclang" "-target-feature" "-Xclang" "+vortex" "-emit-llvm" "-D_CL_DISABLE_HALF"
# "-march=rv32imafd" "-mabi=ilp32f" "-v" "-O3" "-Xclang" "-target-feature" "-Xclang" "+vortex" "-fno-rtti" "-fdata-sections" "-ffunction-sections"
)

set(KERNEL_CL_FLAGS "-Xclang" "-cl-std=CL${VORTEX_DEVICE_CL_STD}" "-D__OPENCL_C_VERSION__=${VORTEX_DEVICE_CL_VERSION}" ${KERNEL_CL_FLAGS})

set(LLC_FLAGS "")

set(DEVICE_CL_FLAGS "-D__OPENCL_VERSION__=${VORTEX_DEVICE_CL_VERSION} -DPOCL_DEVICE_ADDRESS_BITS=32")
set(DEVICE_CL_FLAGS "-D__OPENCL_VERSION__=${VORTEX_DEVICE_CL_VERSION} -DPOCL_DEVICE_ADDRESS_BITS=32 -Dcl_khr_int64")
separate_arguments(VORTEX_DEVICE_EXTENSIONS)
foreach(EXT ${VORTEX_DEVICE_EXTENSIONS})
set(DEVICE_CL_FLAGS "${DEVICE_CL_FLAGS} -D${EXT}")
Expand Down
2 changes: 2 additions & 0 deletions lib/llvmopencl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ set(LLVMPASSES_SOURCES "AllocasToEntry.cc"
"WorkitemReplication.h"
"VortexPrintf.cc"
"VortexBarrier.cc"
"VortexAttribute.cc"
"PrintModule.cc"
)

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${LLVM_CFLAGS}")
Expand Down
52 changes: 52 additions & 0 deletions lib/llvmopencl/PrintModule.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <iostream>
#include <set>
#include <string>

#include "CompilerWarnings.h"
IGNORE_COMPILER_WARNING("-Wunused-parameter")

#include "config.h"
#include "pocl.h"
#include "pocl_llvm_api.h"

#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/IR/Module.h"
#include "llvm/Pass.h"
#include "llvm/Support/CommandLine.h"
#include <llvm/IR/Instructions.h>


POP_COMPILER_DIAGS

using namespace llvm;

namespace {
class PrintModule : public ModulePass {

public:
static char ID;
PrintModule() : ModulePass(ID) {}

virtual bool runOnModule(Module& M);
};

} // namespace

char PrintModule::ID = 0;
static RegisterPass<PrintModule>
X("print-module",
"print module dump");

bool PrintModule::runOnModule(Module& M)
{
{
std::string str;
llvm::raw_string_ostream ostream { str };
M.print(ostream, nullptr, false);


std::cout << "\n\n============================\n Print Module\n"
<< str << std::endl;
}
return false;
}
106 changes: 106 additions & 0 deletions lib/llvmopencl/VortexAttribute.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#include <iostream>
#include <set>
#include <string>

#include "CompilerWarnings.h"
IGNORE_COMPILER_WARNING("-Wunused-parameter")

#include "config.h"
#include "pocl.h"
#include "pocl_llvm_api.h"

#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/IR/Module.h"
#include "llvm/Pass.h"
#include "llvm/Support/CommandLine.h"
#include <llvm/IR/Instructions.h>

#include "Workgroup.h"

POP_COMPILER_DIAGS

using namespace llvm;

namespace {
class VortexRemoveAttr : public ModulePass {

public:
static char ID;
VortexRemoveAttr() : ModulePass(ID) {}

virtual bool runOnModule(Module& M);
};

} // namespace

char VortexRemoveAttr::ID = 0;
static RegisterPass<VortexRemoveAttr>
X("vortex-mno-riscv-attribute",
"print module dump");

static void recursivelyFind(Function* F, llvm::Attribute attr)
{

for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) {
for (BasicBlock::iterator BI = I->begin(), BE = I->end(); BI != BE; ++BI) {

Instruction* instr = dyn_cast<Instruction>(BI);
if (!llvm::isa<CallInst>(instr))
continue;

CallInst* call_inst = dyn_cast<CallInst>(instr);
Function* callee = call_inst->getCalledFunction();

if ((callee == nullptr) || callee->getName().startswith("llvm."))
continue;

if(callee->hasFnAttribute("target-features")){
callee->removeFnAttr("target-features");
callee->addFnAttr(attr.getKindAsString(), attr.getValueAsString());

}else if(callee->hasFnAttribute("target-cpu")){
callee->removeFnAttr("target-cpu");
}

recursivelyFind(callee, attr);
}
}
return;
}


bool VortexRemoveAttr::runOnModule(Module& M)
{
std::string KernelName;
getModuleStringMetadata(M, "KernelName", KernelName);

if(KernelName == "")
return false;

for (llvm::Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
llvm::Function* F = &*I;
if (F->isDeclaration())
continue;

if (KernelName == F->getName().str() || pocl::Workgroup::isKernelToProcess(*F)) {

/*{
const llvm::AttributeList& attrlist = F->getAttributes();
std::string str;
llvm::raw_string_ostream ostream {str};
attrlist.print(ostream);
std::cerr << "### VortexNoRISCVAttribute Pass running on " << KernelName
<< str
<< std::endl;
}*/

if(F->hasFnAttribute("target-features")){
llvm::Attribute target_features = F->getFnAttribute("target-features");
recursivelyFind(F, target_features);
}

}
}

return true;
}
1 change: 0 additions & 1 deletion lib/llvmopencl/VortexBarrier.cc
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,5 @@ bool VortexBarrierLowering::runOnModule(Module& M)

changed = true;
}

return changed;
}
14 changes: 5 additions & 9 deletions lib/llvmopencl/VortexPrintf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ static RegisterPass<VortexPrintfLowering>
X("vortex-printfs",
"Lower printf to vortex printf function");

#define DEBUG_VORTEX_PRINTF
//#define DEBUG_VORTEX_PRINTF

static void printfLowering(Module& M, Instruction* inst,
std::vector<Instruction *>& need_remove){
Expand Down Expand Up @@ -136,17 +136,13 @@ bool VortexPrintfLowering::runOnModule(Module& M)
recursivelyFind(M, F, need_remove);
}
}

{
std::string str;
llvm::raw_string_ostream ostream { str };
M.print(ostream, nullptr, false);
std::cout << str << std::endl;
}

if(!need_remove.empty())
changed = true;

for(auto inst : need_remove){
inst->eraseFromParent();
}

return changed;
}
2 changes: 1 addition & 1 deletion lib/llvmopencl/WorkitemLoopsVX.cc
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ WorkitemLoops::CreateVortexCMLoop(ParallelRegion& region,
builder.CreateBr(oldExit);

std::cerr << "### VortexLoopGen : End program" << std::endl;
printIR(F);
//printIR(F);

return std::make_pair(forInitBB, loopEndBB);
}

0 comments on commit f47c44c

Please sign in to comment.