Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/support llvm sqrt #244

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions etc/clang_plugin/dumpGimple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,8 @@ namespace llvm
return ci->use_empty() ? assignCode(t, GT(GIMPLE_CALL)) : assignCode(t, GT(GIMPLE_ASSIGN));
case llvm::Intrinsic::fabs:
return assignCode(t, GT(GIMPLE_ASSIGN));
case llvm::Intrinsic::sqrt:
return assignCode(t, GT(GIMPLE_ASSIGN));
case llvm::Intrinsic::rint:
return assignCode(t, GT(GIMPLE_ASSIGN));
case llvm::Intrinsic::fmuladd:
Expand Down Expand Up @@ -729,6 +731,15 @@ namespace llvm
return "fabsl";
fd->print(llvm::errs());
report_fatal_error("Plugin Error");
case llvm::Intrinsic::sqrt:
if(fd->getReturnType()->isFloatTy())
return "sqrtf";
else if(fd->getReturnType()->isDoubleTy())
return "sqrt";
else if(fd->getReturnType()->isFP128Ty())
return "sqrtl";
fd->print(llvm::errs());
report_fatal_error("Plugin Error");
case llvm::Intrinsic::memcpy:
{
auto funType = cast<llvm::FunctionType>(fd->getValueType());
Expand Down Expand Up @@ -5518,6 +5529,7 @@ namespace llvm
switch(id)
{
case llvm::Intrinsic::fabs:
case llvm::Intrinsic::sqrt:
case llvm::Intrinsic::memcpy:
case llvm::Intrinsic::memset:
case llvm::Intrinsic::memmove:
Expand Down
77 changes: 26 additions & 51 deletions etc/clang_plugin/plugin_ASTAnalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,10 @@ namespace clang

std::string GetTypeNameCanonical(const QualType& t, const PrintingPolicy& pp) const
{
auto typeName = t->getCanonicalTypeInternal().getAsString(pp);
std::string typeName;
llvm::raw_string_ostream ss(typeName);
t.getCanonicalType().print(ss, pp);
ss.str();
const auto key = std::string("class ");
const auto constkey = std::string("const class ");
if(typeName.find(key) == 0)
Expand Down Expand Up @@ -339,7 +342,6 @@ namespace clang
for(const auto& location_argument : attr_map)
{
const auto& loc = location_argument.first;
const auto& aattr = location_argument.second;
if((prev.isInvalid() || prev < loc) && (loc < locEnd))
{
fun_arg_attr[fname].insert(attr_map[loc].begin(), attr_map[loc].end());
Expand Down Expand Up @@ -424,24 +426,10 @@ namespace clang
};
const auto manageArray = [&](const ConstantArrayType* CA, bool setInterfaceType) {
auto OrigTotArraySize = CA->getSize();
std::string Dimensions;
if(!setInterfaceType)
{
#if __clang_major__ >= 13
Dimensions = "[" + llvm::toString(OrigTotArraySize, 10, false) + "]";
#else
Dimensions = "[" + OrigTotArraySize.toString(10, false) + "]";
#endif
}
while(CA->getElementType()->isConstantArrayType())
{
CA = cast<ConstantArrayType>(CA->getElementType());
const auto n_el = CA->getSize();
#if __clang_major__ >= 13
Dimensions = Dimensions + "[" + llvm::toString(n_el, 10, false) + "]";
#else
Dimensions = Dimensions + "[" + n_el.toString(10, false) + "]";
#endif
OrigTotArraySize *= n_el;
}
if(setInterfaceType)
Expand All @@ -457,42 +445,39 @@ namespace clang
}
const auto paramTypeRemTD = RemoveTypedef(CA->getElementType());
ParamTypeName = GetTypeNameCanonical(paramTypeRemTD, pp) + " *";
ParamTypeNameOrig =
paramTypeRemTD.getAsString(pp) + (Dimensions == "" ? " *" : " (*)" + Dimensions);
ParamTypeNameOrig = GetTypeNameCanonical(CA->getElementType(), pp) + " *";
ParamTypeInclude = getIncludes(paramTypeRemTD);
};
const auto getSizeInBytes = [&](QualType T) {
const auto getSizeInBytes = [&](QualType T) -> int64_t {
if(T->isIncompleteType() || T->isTemplateTypeParmType() || isa<TemplateSpecializationType>(T))
{
attr_val["SizeInBytes"] = "1";
return;
return 1;
}
attr_val["SizeInBytes"] = std::to_string(FD->getASTContext()
.getTypeInfoDataSizeInChars(T)
.
return FD->getASTContext()
.getTypeInfoDataSizeInChars(T)
.
#if __clang_major__ <= 11
first
first
#else
Width
Width
#endif
.getQuantity());
.getQuantity();
};

if(isa<DecayedType>(argType))
{
const auto DT = cast<DecayedType>(argType)->getOriginalType().IgnoreParens();
getSizeInBytes(DT);
attr_val["SizeInBytes"] = std::to_string(getSizeInBytes(DT));
if(isa<ConstantArrayType>(DT))
{
manageArray(cast<ConstantArrayType>(DT), true);
}
else
{
const auto paramTypeRemTD = RemoveTypedef(argType);
const auto paramTypeOrigTD = argType;
ParamTypeName = GetTypeNameCanonical(paramTypeRemTD, pp);
ParamTypeNameOrig = paramTypeOrigTD.getAsString(pp);
ParamTypeInclude = getIncludes(paramTypeOrigTD);
ParamTypeNameOrig = GetTypeNameCanonical(argType, pp);
ParamTypeInclude = getIncludes(argType);
}
if(attr_val.find("interface_type") != attr_val.end())
{
Expand All @@ -513,20 +498,19 @@ namespace clang
}
else if(argType->isPointerType() || argType->isReferenceType())
{
if(isa<ConstantArrayType>(argType->getPointeeType().IgnoreParens()))
const auto ptdType = argType->getPointeeType().IgnoreParens();
attr_val["SizeInBytes"] = std::to_string(getSizeInBytes(ptdType));
if(isa<ConstantArrayType>(ptdType))
{
getSizeInBytes(argType->getPointeeType().IgnoreParens());
manageArray(cast<ConstantArrayType>(argType->getPointeeType().IgnoreParens()), false);
manageArray(cast<ConstantArrayType>(ptdType), false);
}
else
{
const auto suffix = argType->isPointerType() ? "*" : "&";
const auto paramTypeOrigTD = argType->getPointeeType();
const auto paramTypeRemTD = RemoveTypedef(paramTypeOrigTD);
getSizeInBytes(paramTypeRemTD);
const auto paramTypeRemTD = RemoveTypedef(ptdType);
ParamTypeName = GetTypeNameCanonical(paramTypeRemTD, pp) + suffix;
ParamTypeNameOrig = paramTypeOrigTD.getAsString(pp) + suffix;
ParamTypeInclude = getIncludes(paramTypeOrigTD);
ParamTypeNameOrig = GetTypeNameCanonical(argType, pp);
ParamTypeInclude = getIncludes(ptdType);
}
const auto is_channel_if = ParamTypeName.find("ac_channel<") == 0 ||
ParamTypeName.find("stream<") == 0 ||
Expand All @@ -549,12 +533,11 @@ namespace clang
}
else
{
const auto paramTypeOrigTD = argType;
const auto paramTypeRemTD = RemoveTypedef(argType);
getSizeInBytes(paramTypeRemTD);
attr_val["SizeInBytes"] = std::to_string(getSizeInBytes(paramTypeRemTD));
ParamTypeName = GetTypeNameCanonical(paramTypeRemTD, pp);
ParamTypeNameOrig = paramTypeOrigTD.getAsString(pp);
ParamTypeInclude = getIncludes(paramTypeOrigTD);
ParamTypeNameOrig = GetTypeNameCanonical(argType, pp);
ParamTypeInclude = getIncludes(argType);
if(!argType->isBuiltinType() && !argType->isEnumeralType())
{
interface = "none";
Expand Down Expand Up @@ -722,7 +705,6 @@ namespace clang
std::string pname;
std::string interface;
const auto loc = PragmaTok.getLocation();
const auto filename = PP.getSourceManager().getPresumedLoc(loc, false).getFilename();
const auto end_parse = [&]() {
PP.Lex(Tok);
if(Tok.isNot(tok::eod))
Expand Down Expand Up @@ -849,13 +831,6 @@ namespace clang
D.Report(Tok.getLocation(), D.getCustomDiagID(DiagnosticsEngine::Error, "#pragma HLS_cache %0"))
.AddString(msg);
};
const auto end_parse = [&]() {
PP.Lex(Tok);
if(Tok.isNot(tok::eod))
{
print_error("malformed pragma, expecting end)");
}
};
const auto bundle_parse = [&]() {
PP.Lex(Tok);
if(Tok.isNot(tok::equal))
Expand Down
Loading
Loading