Skip to content

Commit

Permalink
Interface inference upgrade (ferrandi#98)
Browse files Browse the repository at this point in the history
* Missing pointer initialization

* Clang format script update

* Interface infer support upgrade

* Testbench generation update

* Compiler download script failure propagation

* Missing testbench initializers fix

* Testbench initialization fix

* Get function return type helper update

* Interface infer virtual ssa fix

* Interface I/O statements handling fix

* Behavioral helper PrintNode fix

* Verilog address decoding logic fix

* Tree helper PrintType fix
  • Loading branch information
Ansaya authored Dec 1, 2022
1 parent 67da4e6 commit 74d99fb
Show file tree
Hide file tree
Showing 62 changed files with 1,866 additions and 2,034 deletions.
27 changes: 17 additions & 10 deletions .devcontainer/library-scripts/compiler-download.sh
Original file line number Diff line number Diff line change
@@ -1,43 +1,46 @@
#!/bin/bash
set -e

export XZ_OPT='-T0'

install_dir="$1"
shift
compilers_list="$1"
shift

bambuhls_compiler_url="https://release.bambuhls.eu/compiler"
wget_opt="-nv -t 5 -T 5 -O -"
failure_file="${install_dir}/download_failed"

inflate() {
echo "Installing $1 into $2"
case $1 in
clang-12 )
wget https://github.com/llvm/llvm-project/releases/download/llvmorg-12.0.1/clang+llvm-12.0.1-x86_64-linux-gnu-ubuntu-16.04.tar.xz $wget_opt | tar -C $2 -xJf - &
wget https://github.com/llvm/llvm-project/releases/download/llvmorg-12.0.1/clang+llvm-12.0.1-x86_64-linux-gnu-ubuntu-16.04.tar.xz $wget_opt | tar -C $2 -xJf - || touch ${failure_file} &
;;
clang-11 )
wget https://github.com/llvm/llvm-project/releases/download/llvmorg-11.1.0/clang+llvm-11.1.0-x86_64-linux-gnu-ubuntu-16.04.tar.xz $wget_opt | tar -C $2 -xJf - &
wget https://github.com/llvm/llvm-project/releases/download/llvmorg-11.1.0/clang+llvm-11.1.0-x86_64-linux-gnu-ubuntu-16.04.tar.xz $wget_opt | tar -C $2 -xJf - || touch ${failure_file} &
;;
clang-10 )
wget https://github.com/llvm/llvm-project/releases/download/llvmorg-10.0.1/clang+llvm-10.0.1-x86_64-linux-gnu-ubuntu-16.04.tar.xz $wget_opt | tar -C $2 -xJf - &
wget https://github.com/llvm/llvm-project/releases/download/llvmorg-10.0.1/clang+llvm-10.0.1-x86_64-linux-gnu-ubuntu-16.04.tar.xz $wget_opt | tar -C $2 -xJf - || touch ${failure_file} &
;;
clang-9 )
wget https://github.com/llvm/llvm-project/releases/download/llvmorg-9.0.1/clang+llvm-9.0.1-x86_64-linux-gnu-ubuntu-16.04.tar.xz $wget_opt | tar -C $2 -xJf - &
wget https://github.com/llvm/llvm-project/releases/download/llvmorg-9.0.1/clang+llvm-9.0.1-x86_64-linux-gnu-ubuntu-16.04.tar.xz $wget_opt | tar -C $2 -xJf - || touch ${failure_file} &
;;
clang-8 )
wget https://releases.llvm.org/8.0.0/clang+llvm-8.0.0-x86_64-linux-gnu-ubuntu-16.04.tar.xz $wget_opt | tar -C $2 -xJf - &
wget https://releases.llvm.org/8.0.0/clang+llvm-8.0.0-x86_64-linux-gnu-ubuntu-16.04.tar.xz $wget_opt | tar -C $2 -xJf - || touch ${failure_file} &
;;
clang-7 )
wget https://releases.llvm.org/7.0.1/clang+llvm-7.0.1-x86_64-linux-gnu-ubuntu-16.04.tar.xz $wget_opt | tar -C $2 -xJf - &
wget https://releases.llvm.org/7.0.1/clang+llvm-7.0.1-x86_64-linux-gnu-ubuntu-16.04.tar.xz $wget_opt | tar -C $2 -xJf - || touch ${failure_file} &
;;
clang-6 )
wget https://releases.llvm.org/6.0.1/clang+llvm-6.0.1-x86_64-linux-gnu-ubuntu-16.04.tar.xz $wget_opt | tar -C $2 -xJf - &
wget https://releases.llvm.org/6.0.1/clang+llvm-6.0.1-x86_64-linux-gnu-ubuntu-16.04.tar.xz $wget_opt | tar -C $2 -xJf - || touch ${failure_file} &
;;
clang-5 )
wget https://releases.llvm.org/5.0.2/clang+llvm-5.0.2-x86_64-linux-gnu-ubuntu-16.04.tar.xz $wget_opt | tar -C $2 -xJf - &
wget https://releases.llvm.org/5.0.2/clang+llvm-5.0.2-x86_64-linux-gnu-ubuntu-16.04.tar.xz $wget_opt | tar -C $2 -xJf - || touch ${failure_file} &
;;
clang-4 )
wget https://releases.llvm.org/4.0.0/clang+llvm-4.0.0-x86_64-linux-gnu-ubuntu-16.04.tar.xz $wget_opt | tar -C $2 -xJf - &
wget https://releases.llvm.org/4.0.0/clang+llvm-4.0.0-x86_64-linux-gnu-ubuntu-16.04.tar.xz $wget_opt | tar -C $2 -xJf - || touch ${failure_file} &
;;
gcc-4.5 )
wget ${bambuhls_compiler_url}/gcc-4.5-bambu-Ubuntu_16.04.tar.xz $wget_opt | tar -C $2 -xJf -
Expand Down Expand Up @@ -74,6 +77,10 @@ IFS=',' read -r -a compilers <<< "${compilers_list}"
compilers=( $(IFS=$'\n'; echo "${compilers[*]}" | sort -V) )
for compiler in "${compilers[@]}"
do
inflate $compiler $install_dir
inflate $compiler $install_dir
done
wait

if [ -e "${failure_file}" ]; then
exit -1
fi
15 changes: 3 additions & 12 deletions clang-format-all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,16 @@ if [ $# -eq 0 ]; then
fi

# Variable that will hold the name of the clang-format command
FMT=""

# Some distros just call it clang-format. Others (e.g. Ubuntu) are insistent
# that the version number be part of the command. We prefer clang-format if
# that's present, otherwise we work backwards from highest version to lowest
# version.
for clangfmt in clang-format{,-{4,3}.{9,8,7,6,5,4,3,2,1,0}}; do
if which "$clangfmt" &>/dev/null; then
FMT="$clangfmt"
break
fi
done
FMT="$(find $(echo $PATH | sed 's/:/ /g') -regextype posix-extended -regex '.*/clang-format-[0-9]{2}' | sort --version-sort --field-separator=- --key=2,2 | tail -n1)"

# Check if we found a working clang-format
if [ -z "$FMT" ]; then
echo "failed to find clang-format"
exit 1
fi

echo "Clang Format: $($FMT --version)"

# Check all of the arguments first to make sure they're all directories
for dir in "$@"; do
if [ ! -d "${dir}" ]; then
Expand Down
108 changes: 67 additions & 41 deletions etc/clang_plugin/plugin_ASTAnalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
#include "clang/Sema/Sema.h"
#include "llvm/Support/raw_ostream.h"

#define PRINT_DBG_MSG 0

static std::map<std::string, std::map<clang::SourceLocation, std::map<std::string, std::string>>> file_loc_attr;
static std::map<std::string, std::map<clang::SourceLocation, std::map<std::string, std::map<std::string, std::string>>>>
file_loc_arg_attr;
Expand Down Expand Up @@ -287,7 +289,8 @@ namespace clang

void AnalyzeFunctionDecl(const FunctionDecl* FD)
{
const auto print_error = [&](const std::string& msg) {
const auto print_error = [&](const std::string& msg)
{
auto& D = CI.getDiagnostics();
D.Report(D.getCustomDiagID(DiagnosticsEngine::Error, "%0")).AddString(msg);
};
Expand All @@ -303,7 +306,8 @@ namespace clang
const auto loc_arg = file_loc_arg_attr.find(filename);
if(loc_arg != file_loc_arg_attr.end())
{
const auto prev = [&]() -> SourceLocation {
const auto prev = [&]() -> SourceLocation
{
const auto prev_it = prevLoc.find(filename);
if(prev_it != prevLoc.end())
{
Expand All @@ -325,7 +329,8 @@ namespace clang
const auto loc_attr = file_loc_attr.find(filename);
if(loc_attr != file_loc_attr.end())
{
const auto prev = [&]() -> SourceLocation {
const auto prev = [&]() -> SourceLocation
{
const auto prev_it = prevLoc.find(filename);
if(prev_it != prevLoc.end())
{
Expand All @@ -348,30 +353,63 @@ namespace clang
if(!FD->isVariadic() && FD->hasBody())
{
Fun2Demangled[fname] = FD->getNameInfo().getName().getAsString();
// llvm::errs() << "function: " << fname << "\n";
#if PRINT_DBG_MSG
llvm::errs() << "function: " << fname << "\n";
#endif

auto par_index = 0u;
for(const auto par : FD->parameters())
{
if(const auto PVD = dyn_cast<ParmVarDecl>(par))
{
const auto pname = [&]() {
const auto pname = [&]()
{
const auto name = PVD->getNameAsString();
if(name.empty())
{
return "P" + std::to_string(par_index);
}
return name;
}();
// llvm::errs() << " arg: " << pname << "\n";
#if PRINT_DBG_MSG
llvm::errs() << " arg: " << pname << "\n";
#endif

auto& attr_val = fun_arg_attr[fname][pname];
const auto argType = PVD->getType();
std::string interface = "default";
std::string ParamTypeName;
std::string ParamTypeNameOrig;
std::string ParamTypeInclude;
const auto manageArray = [&](const ConstantArrayType* CA, bool setInterfaceType) {
const auto getIncludes = [&](const clang::QualType& type)
{
std::string includes;
if(const auto BTD = getBaseTypeDecl(type))
{
includes = SM.getPresumedLoc(BTD->getSourceRange().getBegin(), false).getFilename();
}
const auto tmpl_decl =
llvm::dyn_cast_or_null<ClassTemplateSpecializationDecl>(type->getAsTagDecl());
if(tmpl_decl)
{
const auto& args = tmpl_decl->getTemplateArgs();
for(auto i = 0U; i < args.size(); ++i)
{
const auto& argT = args[i];
if(argT.getKind() == TemplateArgument::ArgKind::Type)
{
if(const auto BTD = getBaseTypeDecl(argT.getAsType()))
{
includes += std::string(";") +
SM.getPresumedLoc(BTD->getSourceRange().getBegin(), false).getFilename();
}
}
}
}
return includes;
};
const auto manageArray = [&](const ConstantArrayType* CA, bool setInterfaceType)
{
auto OrigTotArraySize = CA->getSize();
std::string Dimensions;
if(!setInterfaceType)
Expand All @@ -389,10 +427,7 @@ namespace clang
ParamTypeName = GetTypeNameCanonical(paramTypeRemTD, pp) + " *";
ParamTypeNameOrig =
paramTypeRemTD.getAsString(pp) + (Dimensions == "" ? " *" : " (*)" + Dimensions);
if(const auto BTD = getBaseTypeDecl(paramTypeRemTD))
{
ParamTypeInclude = SM.getPresumedLoc(BTD->getSourceRange().getBegin(), false).getFilename();
}
ParamTypeInclude = getIncludes(paramTypeRemTD);
if(setInterfaceType)
{
interface = "array";
Expand All @@ -413,10 +448,7 @@ namespace clang
const auto paramTypeRemTD = RemoveTypedef(argType);
ParamTypeName = GetTypeNameCanonical(paramTypeRemTD, pp);
ParamTypeNameOrig = paramTypeRemTD.getAsString(pp);
if(const auto BTD = getBaseTypeDecl(paramTypeRemTD))
{
ParamTypeInclude = SM.getPresumedLoc(BTD->getSourceRange().getBegin(), false).getFilename();
}
ParamTypeInclude = getIncludes(paramTypeRemTD);
}
if(attr_val.find("interface_type") != attr_val.end())
{
Expand Down Expand Up @@ -448,32 +480,22 @@ namespace clang
const auto paramTypeRemTD = RemoveTypedef(PT->getPointeeType());
ParamTypeName = GetTypeNameCanonical(paramTypeRemTD, pp) + " *";
ParamTypeNameOrig = paramTypeRemTD.getAsString(pp) + " *";
if(const auto BTD = getBaseTypeDecl(paramTypeRemTD))
{
ParamTypeInclude =
SM.getPresumedLoc(BTD->getSourceRange().getBegin(), false).getFilename();
}
ParamTypeInclude = getIncludes(paramTypeRemTD);
}
}
else if(const auto RT = dyn_cast<ReferenceType>(argType))
{
const auto paramTypeRemTD = RemoveTypedef(RT->getPointeeType());
ParamTypeName = GetTypeNameCanonical(paramTypeRemTD, pp) + " &";
ParamTypeNameOrig = paramTypeRemTD.getAsString(pp) + " &";
if(const auto BTD = getBaseTypeDecl(paramTypeRemTD))
{
ParamTypeInclude = SM.getPresumedLoc(BTD->getSourceRange().getBegin(), false).getFilename();
}
ParamTypeInclude = getIncludes(paramTypeRemTD);
}
else
{
const auto paramTypeRemTD = RemoveTypedef(argType);
ParamTypeName = GetTypeNameCanonical(paramTypeRemTD, pp);
ParamTypeNameOrig = paramTypeRemTD.getAsString(pp);
if(const auto BTD = getBaseTypeDecl(paramTypeRemTD))
{
ParamTypeInclude = SM.getPresumedLoc(BTD->getSourceRange().getBegin(), false).getFilename();
}
ParamTypeInclude = getIncludes(paramTypeRemTD);
}
interface = "ptrdefault";
if(attr_val.find("interface_type") != attr_val.end())
Expand All @@ -495,10 +517,7 @@ namespace clang
const auto paramTypeRemTD = RemoveTypedef(argType);
ParamTypeName = GetTypeNameCanonical(paramTypeRemTD, pp);
ParamTypeNameOrig = paramTypeRemTD.getAsString(pp);
if(const auto BTD = getBaseTypeDecl(paramTypeRemTD))
{
ParamTypeInclude = SM.getPresumedLoc(BTD->getSourceRange().getBegin(), false).getFilename();
}
ParamTypeInclude = getIncludes(paramTypeRemTD);
if(!argType->isBuiltinType() && !argType->isEnumeralType())
{
interface = "none";
Expand All @@ -525,10 +544,12 @@ namespace clang
attr_val["interface_typename"] = ParamTypeName;
attr_val["interface_typename_orig"] = ParamTypeNameOrig;
attr_val["interface_typename_include"] = ParamTypeInclude;
// llvm::errs() << " interface_type = " << interface << "\n";
// llvm::errs() << " interface_typename = " << ParamTypeName << "\n";
// llvm::errs() << " interface_typename_orig = " << ParamTypeNameOrig << "\n";
// llvm::errs() << " interface_typename_include = " << ParamTypeInclude << "\n";
#if PRINT_DBG_MSG
llvm::errs() << " interface_type : " << interface << "\n";
llvm::errs() << " interface_typename : " << ParamTypeName << "\n";
llvm::errs() << " interface_typename_orig : " << ParamTypeNameOrig << "\n";
llvm::errs() << " interface_typename_include: " << ParamTypeInclude << "\n";
#endif
}
++par_index;
}
Expand Down Expand Up @@ -601,7 +622,8 @@ namespace clang
Token& PragmaTok) override
{
Token Tok{};
const auto print_error = [&](const std::string& msg) {
const auto print_error = [&](const std::string& msg)
{
auto& D = PP.getDiagnostics();
D.Report(Tok.getLocation(), D.getCustomDiagID(DiagnosticsEngine::Error, "#pragma HLS_interface %0"))
.AddString(msg);
Expand All @@ -610,14 +632,16 @@ namespace clang
std::string interface;
const auto loc = PragmaTok.getLocation();
const auto filename = PP.getSourceManager().getPresumedLoc(loc, false).getFilename();
const auto end_parse = [&]() {
const auto end_parse = [&]()
{
PP.Lex(Tok);
if(Tok.isNot(tok::eod))
{
print_error("malformed");
}
};
const auto bundle_parse = [&]() {
const auto bundle_parse = [&]()
{
PP.Lex(Tok);
if(Tok.is(tok::equal))
{
Expand All @@ -631,7 +655,8 @@ namespace clang
file_loc_arg_attr[filename][loc][pname]["attribute3"] = bundle;
// llvm::errs() << " bundle=" << bundle;
};
const auto array_parse = [&]() {
const auto array_parse = [&]()
{
PP.Lex(Tok);
if(Tok.isNot(tok::numeric_constant))
{
Expand All @@ -652,7 +677,8 @@ namespace clang
print_error("array malformed");
}
};
const auto axi_parse = [&]() {
const auto axi_parse = [&]()
{
PP.Lex(Tok);
if(Tok.isNot(tok::identifier))
{
Expand Down
2 changes: 1 addition & 1 deletion etc/lib/technology/C_MEM_IPs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5572,7 +5572,7 @@ generate
if(USE_SPARSE_MEMORY==1)
assign relative_addr = tmp_addr[nbit_addr-1:0];
else
assign relative_addr = tmp_addr-address_space_begin[nbit_addr-1:0];
assign relative_addr = tmp_addr-address_space_begin[((nbit_addr-1) &lt; 32 ? (nbit_addr-1) : 31):0];
endgenerate

generate
Expand Down
Loading

0 comments on commit 74d99fb

Please sign in to comment.