Skip to content

Commit

Permalink
Hilapp now compiles with clang-12 - look for __clang__ -macros to see…
Browse files Browse the repository at this point in the history
… version dependent bits

Former-commit-id: 7991e342a0c41aa0423fd9bcb6345c28c1ccc56e
  • Loading branch information
KariRummukainen committed Mar 4, 2021
1 parent ef7cd5b commit 1a38f4e
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 53 deletions.
7 changes: 4 additions & 3 deletions hilapp/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,11 @@ endif
# These are required when compiling vs. a source distribution of Clang. For
# binary distributions llvm-config --cxxflags gives the right path.
#
#CLANG_INCLUDES := \
# CLANG_INCLUDES := \
# -I$(LLVM_SRC_PATH)/include \
# -I$(LLVM_SRC_PATH)/tools/clang/include \
# -I$(LLVM_BUILD_PATH)/tools/clang/include
# -I$(LLVM_BUILD_PATH)/tools/clang/include \
# -I$(LLVM_SRC_PATH)/tools/clang/include



# List of Clang libraries to link. The proper -L will be provided by the
Expand Down
2 changes: 1 addition & 1 deletion hilapp/src/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ void TopLevelVisitor::generate_code(Stmt *S) {

if (cmdline::check_initialization) {

std::string fname = srcMgr.getFilename(S->getSourceRange().getBegin());
std::string fname = srcMgr.getFilename(S->getSourceRange().getBegin()).str();
code << "if (!" << l.new_name << ".is_initialized(" << init_par
<< ")){\noutput0 << \"File " << fname << " on line "
<< srcMgr.getSpellingLineNumber(S->getSourceRange().getBegin())
Expand Down
1 change: 0 additions & 1 deletion hilapp/src/hilapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ std::list<field_info> field_info_list = {};
std::list<array_ref> array_ref_list = {};
std::list<vector_reduction_ref> vector_reduction_ref_list = {};
std::list<special_function_call> special_function_call_list = {};
std::vector<Expr *> remove_expr_list = {};

bool state::compile_errors_occurred = false;

Expand Down
24 changes: 11 additions & 13 deletions hilapp/src/hilapp.h
Original file line number Diff line number Diff line change
Expand Up @@ -257,23 +257,22 @@ struct var_info {
}
};

/// Store non-var code sections which can be replaced. These happen thus far pretty much
/// only with array references

struct replace_expr {
Expr * E; // expression to be replaced
std::string type; // type of expression
std::string repl_name; // name of the replace

}

/// Stores onformation for a single reference to an array
/// Stores information for a single reference to an loop-extern array or related variable
/// These are similar to variable references, but often
/// need to be handled differently
struct array_ref {
ArraySubscriptExpr *ref;
std::string new_name;
Expr *E; // the whole expression
ArraySubscriptExpr *ref; // as type says
std::string new_name;
std::string type;
bool replace_expr_with_var; // if true replace the whole expression with variable

array_ref() {
E = nullptr;
ref = nullptr;
replace_expr_with_var = false;
}
};

/// store necessary information for vector reductions
Expand Down Expand Up @@ -439,6 +438,5 @@ extern std::list<field_info> field_info_list;
extern std::list<array_ref> array_ref_list;
extern std::list<vector_reduction_ref> vector_reduction_ref_list;
extern std::list<special_function_call> special_function_call_list;
extern std::vector<Expr *> remove_expr_list;

#endif
91 changes: 59 additions & 32 deletions hilapp/src/toplevelvisitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#include "hilapp.h"
#include "toplevelvisitor.h"
#include "specialization_db.h"
#include "clang/Analysis/CallGraph.h"
#include <sstream>
#include <iostream>
#include <string>
Expand Down Expand Up @@ -230,9 +229,17 @@ bool TopLevelVisitor::handle_field_X_expr(Expr *e, bool is_assign, bool is_also_
}

Expr *dirE = Op->getArg(1)->IgnoreImplicit();
llvm::APSInt result;
if (dirE->isIntegerConstantExpr(result, *Context)) {
// Got constant
if (dirE->isIntegerConstantExpr(*Context)) {
llvm::APSInt result;

// Got constant -- interface changes in clang 12 or 13(!)
#if defined(__clang_major__) && (__clang_major__ <= 11)
dirE->isIntegerConstantExpr(result,*Context);
#else
auto res = dirE->getIntegerConstantExpr(*Context);
result = res.getValue();
#endif

lfe.is_constant_direction = true;
lfe.constant_value = result.getExtValue();
// llvm::errs() << " GOT DIR CONST, value " << lfe.constant_value << "
Expand Down Expand Up @@ -322,16 +329,16 @@ bool TopLevelVisitor::is_variable_loop_local(VarDecl *decl) {
return false;
}

/// handle an array subscript expression
/// Check if array itself is loop extern or intern
/// -> if intern, nothing special needs to be done
/// -> if extern, check index:
/// -> if site dep, mark loop_info.has_site_dep_cond_or_index
/// -> if contains loop local var ref || site dep:
/// -> whole array is input to loop: need to know array size
/// If size not knowable, flag error
/// -> if !site dep and !loop local:
/// -> it is sufficient to read in this array element only,
/// handle an array subscript expression. Operations depend
/// on whether the array is defined loop extern or intern:
/// - if intern, nothing special needs to be done
/// - if extern, check index:
/// - if site dependent, mark loop_info.has_site_dep_cond_or_index
/// - if contains loop local var ref or is site dependent:
/// whole array is input to loop: need to know array size.
/// If size not knowable, flag error
/// - If index is not site dependent or loop local:
/// - it is sufficient to read in this array element only,
/// and remove var references to variables in index
///
int TopLevelVisitor::handle_array_var_ref(ArraySubscriptExpr *E, bool is_assign,
Expand All @@ -346,10 +353,12 @@ int TopLevelVisitor::handle_array_var_ref(ArraySubscriptExpr *E, bool is_assign,
return 0;
}

llvm::errs() << " %% Inside array reference\n";

// Check if it's local
VarDecl *decl = dyn_cast<VarDecl>(DRE->getDecl());
VarDecl *vd = dyn_cast<VarDecl>(DRE->getDecl());

if (is_variable_loop_local(decl)) {
if (is_variable_loop_local(vd)) {
// if array is declared within the loop, nothing special needs to
// be done. Let us anyway put it through the std handler in order to
// flag it.
Expand Down Expand Up @@ -382,28 +391,44 @@ int TopLevelVisitor::handle_array_var_ref(ArraySubscriptExpr *E, bool is_assign,
// Note: multiple refrences are not checked, thus, same element can be
// referred more than once. TODO? (small optimization)

// misusing the var_info_list here
var_info vi;
// use the array_ref_list to note this
array_ref ar;
ar.replace_expr_with_var = true;
ar.E = E;
array_ref_list.push_back(ar);

llvm::errs() << " %%% found fully extern array reference\n";

parsing_state.skip_children = 1; // no need to look inside the replacement
// variable refs inside should not be recorded
return 1;
}
if (!site_dep) {
bool index_local = contains_loop_local_var(E->getIdx(), nullptr);

if (!index)
}
// Now there is site dep/loop local stuff in index. Whole array has to be taken
// "in".

const ConstantArrayType *cat = Context->getAsConstantArrayType(vd->getType());
if (cat) {
llvm::errs() << " %%% Found constant array type expr, size " << cat->getSize()
<< "\n";

if (!)
DRE = dyn_cast<DeclRefExpr>(E->getIdx()->IgnoreImplicit());
if (DRE) {
decl = dyn_cast<VarDecl>(DRE->getDecl());
index_local = is_variable_loop_local(decl);
} else {
// This happens when the index is not a variable.
// It's probably a compile time constant
index_local = true;
const VariableArrayType *vat = Context->getAsVariableArrayType(vd->getType());
if (vat) {
llvm::errs() << " %%% Found variable array type expr, size "
<< get_stmt_str(vat->getSizeExpr()) << "\n";
} else {

// Now different array type - flag as error
reportDiag(DiagnosticsEngine::Level::Error, E->getSourceRange().getBegin(),
"Array size is unknown - recommend use of Vector<>, "
"std::array<> or std::vector<> instead");
return 1;
}
}

#if 0

// Now handling depends on wether it's local, global, or something else
if (!array_local) {
// llvm::errs() << "Non-local array\n";
Expand Down Expand Up @@ -445,6 +470,9 @@ int TopLevelVisitor::handle_array_var_ref(ArraySubscriptExpr *E, bool is_assign,
// Array and index are local. This does not require any action.
}
}

#endif

return 1;
}

Expand Down Expand Up @@ -481,7 +509,6 @@ bool TopLevelVisitor::handle_full_loop_stmt(Stmt *ls, bool field_parity_ok) {
parsing_state.in_loop_body = false;
parsing_state.ast_depth = 0;


// check and analyze the field expressions
check_var_info_list();
check_addrofops_and_refs(ls); // scan through the full loop again
Expand Down Expand Up @@ -1247,7 +1274,7 @@ void TopLevelVisitor::ast_dump_header(const char *s, const SourceRange sr_in,
bool is_function) {
SourceRange sr = sr_in;
unsigned linenumber = srcMgr.getSpellingLineNumber(sr.getBegin());
std::string name = srcMgr.getFilename(sr.getBegin());
std::string name = srcMgr.getFilename(sr.getBegin()).str();

// check if it is macro
if (sr.getBegin().isMacroID()) {
Expand Down
6 changes: 3 additions & 3 deletions howto/HOWTO_GET_CLANG
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ git clone https://github.com/llvm/llvm-project.git
cd llvm-project
mkdir build
cd build
cmake -G "Unix Makefiles" -DLLVM_ENABLE_PROJECTS=clang -DCMAKE_BUILD_TYPE=Release ../llvm
make -j4
cmake -G "Unix Makefiles" -DLLVM_ENABLE_PROJECTS=clang -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/install/dir ../llvm
make -j4 # compiles everything
make install # installs to /install/dir, default /usr/local

* This leaves binaries in llvm-project/build/bin -directory.
* Build types are Release, MinSizeRel, Debug, RelWithDebInfo


0 comments on commit 1a38f4e

Please sign in to comment.