From f8645484681f4a1109a81da0fb7d13d1972cdeac Mon Sep 17 00:00:00 2001 From: Dominik Adamski Date: Tue, 12 Apr 2022 09:30:29 +0200 Subject: [PATCH] Fix reduction failures. Signed-off-by: Dominik Adamski --- tools/flang2/flang2exe/exp_ftn.cpp | 10 +++++-- tools/flang2/flang2exe/exp_ftn.h | 6 +++- tools/flang2/flang2exe/exp_rte.cpp | 4 +-- tools/flang2/flang2exe/exp_rte.h | 6 +++- tools/flang2/flang2exe/expand.cpp | 47 ++++++++++++++++++++++++++++-- tools/flang2/flang2exe/ilmutil.cpp | 10 +++++++ tools/flang2/flang2exe/ilmutil.h | 10 +++++++ tools/shared/expand.h | 2 ++ 8 files changed, 85 insertions(+), 10 deletions(-) diff --git a/tools/flang2/flang2exe/exp_ftn.cpp b/tools/flang2/flang2exe/exp_ftn.cpp index 9de771c26e..d61ed99215 100644 --- a/tools/flang2/flang2exe/exp_ftn.cpp +++ b/tools/flang2/flang2exe/exp_ftn.cpp @@ -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; @@ -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: diff --git a/tools/flang2/flang2exe/exp_ftn.h b/tools/flang2/flang2exe/exp_ftn.h index 4832002ca6..3e138ee9ae 100644 --- a/tools/flang2/flang2exe/exp_ftn.h +++ b/tools/flang2/flang2exe/exp_ftn.h @@ -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 ... diff --git a/tools/flang2/flang2exe/exp_rte.cpp b/tools/flang2/flang2exe/exp_rte.cpp index 744a8c28b0..ed0f2faefb 100644 --- a/tools/flang2/flang2exe/exp_rte.cpp +++ b/tools/flang2/flang2exe/exp_rte.cpp @@ -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; @@ -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); diff --git a/tools/flang2/flang2exe/exp_rte.h b/tools/flang2/flang2exe/exp_rte.h index 7d8394c9da..967b84311a 100644 --- a/tools/flang2/flang2exe/exp_rte.h +++ b/tools/flang2/flang2exe/exp_rte.h @@ -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 ... diff --git a/tools/flang2/flang2exe/expand.cpp b/tools/flang2/flang2exe/expand.cpp index 7262f8653e..90fad81a43 100644 --- a/tools/flang2/flang2exe/expand.cpp +++ b/tools/flang2/flang2exe/expand.cpp @@ -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); @@ -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 @@ -237,6 +274,7 @@ expand(void) static std::map process_expanded_map = std::map(); 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) @@ -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 */ @@ -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 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)); } @@ -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); diff --git a/tools/flang2/flang2exe/ilmutil.cpp b/tools/flang2/flang2exe/ilmutil.cpp index 514c161fad..f47ce47ee9 100644 --- a/tools/flang2/flang2exe/ilmutil.cpp +++ b/tools/flang2/flang2exe/ilmutil.cpp @@ -2071,3 +2071,13 @@ count_ilms() return newnumilms; } +int get_gilmb_ilmpos() +{ + return gilmb.ilmpos; +} + +void set_gilmb_ilmpos(int val) +{ + gilmb.ilmpos = val; +} + diff --git a/tools/flang2/flang2exe/ilmutil.h b/tools/flang2/flang2exe/ilmutil.h index 2e8be8e721..cd366aa945 100644 --- a/tools/flang2/flang2exe/ilmutil.h +++ b/tools/flang2/flang2exe/ilmutil.h @@ -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_ diff --git a/tools/shared/expand.h b/tools/shared/expand.h index 156d5449c0..6c579aa085 100644 --- a/tools/shared/expand.h +++ b/tools/shared/expand.h @@ -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