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

Ported integration_tests/arrays_op_19.f90 from LFortran and improve LC to support it #39

Merged
merged 4 commits into from
Dec 27, 2023
Merged
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
2 changes: 2 additions & 0 deletions integration_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,5 @@ RUN(NAME array_08.cpp LABELS gcc llvm NOFAST
EXTRA_ARGS --extra-arg=-I${CONDA_PREFIX}/include)
RUN(NAME array_09.cpp LABELS gcc llvm NOFAST
EXTRA_ARGS --extra-arg=-I${CONDA_PREFIX}/include)
RUN(NAME array_10.cpp LABELS gcc llvm NOFAST
EXTRA_ARGS --extra-arg=-I${CONDA_PREFIX}/include)
34 changes: 34 additions & 0 deletions integration_tests/array_10.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <iostream>
#include <xtensor/xtensor.hpp>
#include <xtensor/xfixed.hpp>
#include "xtensor/xio.hpp"
#include "xtensor/xview.hpp"

xt::xtensor<double, 2> softmax(xt::xtensor<double, 2>& x) {
xt::xtensor<double, 2> y = xt::empty<double>({x.shape(0), x.shape(1)});
int i;
double s;

for( int i = 0; i < x.shape(1); i++ ) {
xt::view(y, xt::all(), i) = xt::exp(xt::view(x, xt::all(), i) - xt::amax(xt::view(x, xt::all(), i)));
xt::view(y, xt::all(), i) = xt::view(y, xt::all(), i) / xt::sum(xt::view(y, xt::all(), i));
}

return y;
}

int main() {

xt::xtensor<double, 2> x = xt::empty<double>({10, 10});
// xt::xtensor<double, 2> sx = xt::empty<double>({10, 10}); // Uncomment, bug in IntrinsicScalarFunctions::Abs
xt::xtensor_fixed<double, xt::xshape<10, 10>> sx;

x.fill(2.0);

sx = softmax(x);
std::cout<< sx << std::endl;
if( xt::any(xt::abs(sx - 0.1) > 1e-6) ) {
exit(2);
}

}
88 changes: 73 additions & 15 deletions src/lc/clang_ast_to_asr.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ enum SpecialFunc {
All,
Any,
NotEqual,
Exp,
Abs,
AMax,
Sum,
};

std::map<std::string, SpecialFunc> special_function_map = {
Expand All @@ -52,6 +56,10 @@ std::map<std::string, SpecialFunc> special_function_map = {
{"all", SpecialFunc::All},
{"any", SpecialFunc::Any},
{"not_equal", SpecialFunc::NotEqual},
{"exp", SpecialFunc::Exp},
{"abs", SpecialFunc::Abs},
{"amax", SpecialFunc::AMax},
{"sum", SpecialFunc::Sum},
};

class OneTimeUseString {
Expand Down Expand Up @@ -501,6 +509,20 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
throw std::runtime_error(cxx_operator_name + " is supported only for arrays."); \
} \

#define generate_code_for_cmpop(op) if( x->getNumArgs() != 2 ) { \
throw std::runtime_error(cxx_operator_name + " accepts two arguments, found " + std::to_string(x->getNumArgs())); \
} \
clang::Expr** args = x->getArgs(); \
TraverseStmt(args[0]); \
ASR::expr_t* obj = ASRUtils::EXPR(tmp.get()); \
if( ASRUtils::is_array(ASRUtils::expr_type(obj)) ) { \
TraverseStmt(args[1]); \
ASR::expr_t* value = ASRUtils::EXPR(tmp.get()); \
CreateCompareOp(obj, value, op, Lloc(x)); \
} else { \
throw std::runtime_error(cxx_operator_name + " is supported only for arrays."); \
} \

clang::Expr* callee = x->getCallee();
TraverseStmt(callee);
std::string cxx_operator_name = cxx_operator_name_obj.get();
Expand Down Expand Up @@ -578,9 +600,15 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
throw std::runtime_error("operator= is supported only for arrays.");
}
} else if( cxx_operator_name == "operator+" ) {
generate_code_for_binop(ASR::binopType::Add)
generate_code_for_binop(ASR::binopType::Add);
} else if( cxx_operator_name == "operator-" ) {
generate_code_for_binop(ASR::binopType::Sub);
} else if( cxx_operator_name == "operator/" ) {
generate_code_for_binop(ASR::binopType::Div);
} else if( cxx_operator_name == "operator*" ) {
generate_code_for_binop(ASR::binopType::Mul)
generate_code_for_binop(ASR::binopType::Mul);
} else if( cxx_operator_name == "operator>" ) {
generate_code_for_cmpop(ASR::cmpopType::Gt);
} else {
throw std::runtime_error("C++ operator is not supported yet, " + cxx_operator_name);
}
Expand Down Expand Up @@ -619,11 +647,14 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
skip_format_str = false;
continue;
}
if( tmp == nullptr && is_all_called ) {
if( (tmp == nullptr && is_all_called) ||
(tmp == nullptr || p->getStmtClass() ==
clang::Stmt::StmtClass::CXXDefaultArgExprClass ) ) {
args.push_back(al, nullptr);
is_all_called = false;
} else {
args.push_back(al, ASRUtils::EXPR(tmp.get()));
ASR::asr_t* tmp_ = tmp.get();
args.push_back(al, ASRUtils::EXPR(tmp_));
}
}
assignment_target = assignment_target_copy;
Expand Down Expand Up @@ -718,6 +749,30 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
static_cast<int64_t>(ASRUtils::IntrinsicArrayFunctions::Any),
args.p, args.size(), 0, ASRUtils::TYPE(ASR::make_Logical_t(
al, Lloc(x), 4)), nullptr);
} else if( sf == SpecialFunc::Exp ) {
tmp = ASRUtils::make_IntrinsicScalarFunction_t_util(al, Lloc(x),
static_cast<int64_t>(ASRUtils::IntrinsicScalarFunctions::Exp),
args.p, args.size(), 0, ASRUtils::expr_type(args.p[0]), nullptr);
} else if( sf == SpecialFunc::Abs ) {
tmp = ASRUtils::make_IntrinsicScalarFunction_t_util(al, Lloc(x),
static_cast<int64_t>(ASRUtils::IntrinsicScalarFunctions::Abs),
args.p, args.size(), 0, ASRUtils::expr_type(args.p[0]), nullptr);
} else if( sf == SpecialFunc::AMax ) {
if( args.size() > 1 && args.p[1] != nullptr ) {
throw std::runtime_error("dim argument not yet supported with " + func_name);
}
tmp = ASRUtils::make_IntrinsicArrayFunction_t_util(al, Lloc(x),
static_cast<int64_t>(ASRUtils::IntrinsicArrayFunctions::MaxVal),
args.p, 1, 0, ASRUtils::extract_type(ASRUtils::expr_type(args.p[0])),
nullptr);
} else if( sf == SpecialFunc::Sum ) {
if( args.size() > 1 && args.p[1] != nullptr ) {
throw std::runtime_error("dim argument not yet supported with " + func_name);
}
tmp = ASRUtils::make_IntrinsicArrayFunction_t_util(al, Lloc(x),
static_cast<int64_t>(ASRUtils::IntrinsicArrayFunctions::Sum),
args.p, 1, 0, ASRUtils::extract_type(ASRUtils::expr_type(args.p[0])),
nullptr);
} else if( sf == SpecialFunc::NotEqual ) {
ASR::expr_t* arg1 = args.p[0];
ASR::expr_t* arg2 = args.p[1];
Expand Down Expand Up @@ -1198,19 +1253,22 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
bool TraverseDeclRefExpr(clang::DeclRefExpr* x) {
std::string name = x->getNameInfo().getAsString();
ASR::symbol_t* sym = resolve_symbol(name);
if( name == "operator<<" || name == "cout" ||
name == "endl" || name == "operator()" ||
name == "operator+" || name == "operator=" ||
name == "operator*" || name == "view" ||
name == "empty" || name == "all" ||
name == "any" || name == "not_equal" ||
name == "exit" || name == "printf" ) {
if( sym != nullptr ) {
if( name == "operator<<" || name == "cout" || name == "endl" ||
name == "operator()" || name == "operator+" || name == "operator=" ||
name == "operator*" || name == "view" || name == "empty" ||
name == "all" || name == "any" || name == "not_equal" ||
name == "exit" || name == "printf" || name == "exp" ||
name == "sum" || name == "amax" || name == "abs" ||
name == "operator-" || name == "operator/" || name == "operator>" ) {
if( sym != nullptr && ASR::is_a<ASR::Function_t>(
*ASRUtils::symbol_get_past_external(sym)) ) {
throw std::runtime_error("Special function " + name + " cannot be overshadowed yet.");
}
cxx_operator_name_obj.set(name);
tmp = nullptr;
return true;
if( sym == nullptr ) {
cxx_operator_name_obj.set(name);
tmp = nullptr;
return true;
}
}
if( sym == nullptr ) {
throw std::runtime_error("Symbol " + name + " not found in current scope.");
Expand Down