Skip to content

Commit

Permalink
Fix reduction failures.
Browse files Browse the repository at this point in the history
Signed-off-by: Dominik Adamski <Dominik.Adamski@amd.com>
  • Loading branch information
DominikAdamski committed Apr 12, 2022
1 parent 077b527 commit f864548
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 10 deletions.
10 changes: 7 additions & 3 deletions tools/flang2/flang2exe/exp_ftn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4147,7 +4147,11 @@ exp_bran(ILM_OP opc, ILM *ilmp, int curilm)
/***************************************************************/

void
exp_misc(ILM_OP opc, ILM *ilmp, int curilm, bool process_expanded)
exp_misc(ILM_OP opc,
ILM *ilmp,
int curilm,
bool process_expanded,
bool is_reduction_kernel)
{
int tmp;
int ilix, listilix;
Expand Down Expand Up @@ -4289,11 +4293,11 @@ exp_misc(ILM_OP opc, ILM *ilmp, int curilm, bool process_expanded)
break;

case IM_ENDF:
exp_end(ilmp, curilm, true, process_expanded);
exp_end(ilmp, curilm, true, process_expanded, is_reduction_kernel);
break;

case IM_END:
exp_end(ilmp, curilm, false, process_expanded);
exp_end(ilmp, curilm, false, process_expanded, is_reduction_kernel);
break;

case IM_BYVAL:
Expand Down
6 changes: 5 additions & 1 deletion tools/flang2/flang2exe/exp_ftn.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ void exp_bran(ILM_OP opc, ILM *ilmp, int curilm);
/**
\brief ...
*/
void exp_misc(ILM_OP opc, ILM *ilmp, int curilm, bool process_expanded = false);
void exp_misc(ILM_OP opc,
ILM *ilmp,
int curilm,
bool process_expanded = false,
bool is_reduction_kernel = false);

/**
\brief ...
Expand Down
4 changes: 2 additions & 2 deletions tools/flang2/flang2exe/exp_rte.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2139,7 +2139,7 @@ exp_alloca(ILM *ilmp)
static void gen_funcret(finfo_t *);

void
exp_end(ILM *ilmp, int curilm, bool is_func, bool process_expanded)
exp_end(ILM *ilmp, int curilm, bool is_func, bool process_expanded, bool is_reduction)
{
int tmp;
int op1;
Expand All @@ -2159,7 +2159,7 @@ exp_end(ILM *ilmp, int curilm, bool is_func, bool process_expanded)
if (flg.omptarget && !is_func) {
if (XBIT(232, 0x40) && gbl.ompaccel_intarget && !OMPACCFUNCDEVG(gbl.currsub) /*is_gpu_output_file() */ ) {
OMP_TARGET_MODE mode = ompaccel_tinfo_get(gbl.currsub)->mode;
if (!is_SPMD_mode(mode) && !process_expanded) {
if (is_reduction || (!is_SPMD_mode(mode) && !process_expanded)) {
ilix = ll_make_kmpc_target_deinit(mode);
iltb.callfg = 1;
chk_block(ilix);
Expand Down
6 changes: 5 additions & 1 deletion tools/flang2/flang2exe/exp_rte.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,11 @@ void exp_cgoto(ILM *ilmp, int curilm);
/**
\brief ...
*/
void exp_end(ILM *ilmp, int curilm, bool is_func, bool process_expanded = false);
void exp_end(ILM *ilmp,
int curilm,
bool is_func,
bool process_expanded = false,
bool is_reduction = false);

/**
\brief ...
Expand Down
47 changes: 44 additions & 3 deletions tools/flang2/flang2exe/expand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ static int efunc(const char *);
static int create_ref(SPTR sym, int *pnmex, int basenm, int baseilix,
int *pclen, int *pmxlen, int *prestype);
static int jsr2qjsr(int);
static bool is_reduction_kernel;

SPTR
eval_ilm_check_if_skip(int ilmx, int *skip_expand = nullptr, int *process_expanded = nullptr);
Expand Down Expand Up @@ -218,6 +219,42 @@ parse_im_file(const ILM *ilmp, int *lineno_out, int *findex_out, int *ftag_out)
*ftag_out = ftag;
}

bool target_region_contains_reduction()
{
int curr_expb_nilms = expb.nilms;
int curr_pos = ilmb.globalilmcount;
int curr_ilmpos = get_gilmb_ilmpos();
ILM *ilmp; /* absolute pointer to the ILM */
ILM_OP opc; /* opcode of the ILM */
int len;
do {
expb.nilms = rdilms();
if (expb.nilms > expb.ilmb.stg_size) {
EXP_MORE(expb.ilmb, ILM_AUX, expb.nilms + 100);
}
for (int ilmx = 0; ilmx < expb.nilms; ilmx += len) {
ilmp = (ILM *)(ilmb.ilm_base + ilmx);
opc = ILM_OPC(ilmp);
len = ilms[opc].oprs + 1; /* length is number of words for the
* fixed operands and the opcode */
if (IM_VAR(opc))
len += ILM_OPND(ilmp, 1); /* include the number of
* variable operands */
if (opc == IM_MP_BREDUCTION) {
expb.nilms = curr_expb_nilms;
ilmb.globalilmcount = curr_pos;
set_gilmb_ilmpos(curr_ilmpos);
return true;
}
}
}
while (opc != IM_ETARGET && opc != IM_END && opc != IM_ENDF);
expb.nilms = curr_expb_nilms;
ilmb.globalilmcount = curr_pos;
set_gilmb_ilmpos(curr_ilmpos);
return false;
}

/***************************************************************/
/** \brief Expand ILMs to ILIs */
int
Expand All @@ -237,6 +274,7 @@ expand(void)
static std::map<int, int> process_expanded_map = std::map<int,int>();
auto it = process_expanded_map.find(gbl.currsub);
int process_expanded = 0;
is_reduction_kernel = false;

// we reset flag because we do not know if we generate initialization
// function for SPMD kernel (the function with kmpc_parallel_51 call)
Expand Down Expand Up @@ -740,9 +778,9 @@ eval_ilm_check_if_skip(int ilmx, int *skip_expand, int *process_expanded)

case IMTY_MISC: /* miscellaneous */
if (process_expanded && *process_expanded)
exp_misc(opcx, ilmpx, ilmx, true);
exp_misc(opcx, ilmpx, ilmx, true, is_reduction_kernel);
else
exp_misc(opcx, ilmpx, ilmx);
exp_misc(opcx, ilmpx, ilmx, false, is_reduction_kernel);
break;

case IMTY_FSTR: /* fortran string */
Expand Down Expand Up @@ -787,6 +825,7 @@ eval_ilm_check_if_skip(int ilmx, int *skip_expand, int *process_expanded)
if (XBIT(232, 0x40) && gbl.ompaccel_intarget && !*process_expanded) {
//TODO move initialization to separate function
std::vector<int> allocated_symbols;
bool reduction_pragma = target_region_contains_reduction();
if (is_SPMD_mode(ompaccel_tinfo_get(gbl.currsub)->mode)) {
allocated_symbols = get_allocated_symbols(ompaccel_tinfo_get(gbl.currsub));
}
Expand Down Expand Up @@ -814,8 +853,10 @@ eval_ilm_check_if_skip(int ilmx, int *skip_expand, int *process_expanded)
* will be located */
RFCNTI(target_code_lab);
exp_label(target_code_lab);
if (reduction_pragma)
is_reduction_kernel = true;

if (is_SPMD_mode(ompaccel_tinfo_get(gbl.currsub)->mode)) {
if (is_SPMD_mode(ompaccel_tinfo_get(gbl.currsub)->mode) && !reduction_pragma) {
ilix = ll_make_kmpc_global_thread_num();
iltb.callfg = 1;
chk_block(ilix);
Expand Down
10 changes: 10 additions & 0 deletions tools/flang2/flang2exe/ilmutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2071,3 +2071,13 @@ count_ilms()
return newnumilms;
}

int get_gilmb_ilmpos()
{
return gilmb.ilmpos;
}

void set_gilmb_ilmpos(int val)
{
gilmb.ilmpos = val;
}

10 changes: 10 additions & 0 deletions tools/flang2/flang2exe/ilmutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -248,4 +248,14 @@ void swap_next_gilm(void);
*/
void wrilms(int linenum);

/**
\brief return gbl.ilmpos value
*/
int get_gilmb_ilmpos();

/**
\brief set gilmb.ilmpos value
*/
void set_gilmb_ilmpos(int value);

#endif // ILMUTIL_H_
2 changes: 2 additions & 0 deletions tools/shared/expand.h
Original file line number Diff line number Diff line change
Expand Up @@ -357,4 +357,6 @@ void replace_by_zero(ILM_OP opc, ILM *ilmp, int curilm);
*/
void set_assn(int nme);

bool target_region_contains_reduction();

#endif

0 comments on commit f864548

Please sign in to comment.