From 83e8261148d4b432fd70ecffd1817d1624940eaa Mon Sep 17 00:00:00 2001 From: Remo Dietlicher Date: Tue, 30 Jul 2019 10:42:17 +0200 Subject: [PATCH 1/4] pass down parent module to access symboltable --- .../transformation/ll/caching/Kcaching.java | 35 +++++++++++++------ 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/cx2t/src/claw/wani/transformation/ll/caching/Kcaching.java b/cx2t/src/claw/wani/transformation/ll/caching/Kcaching.java index f3bb02f8b..849e2aa94 100644 --- a/cx2t/src/claw/wani/transformation/ll/caching/Kcaching.java +++ b/cx2t/src/claw/wani/transformation/ll/caching/Kcaching.java @@ -12,6 +12,7 @@ import claw.tatsu.xcodeml.xnode.common.*; import claw.tatsu.xcodeml.xnode.fortran.FbasicType; import claw.tatsu.xcodeml.xnode.fortran.FfunctionDefinition; +import claw.tatsu.xcodeml.xnode.fortran.FmoduleDefinition; import claw.tatsu.xcodeml.xnode.fortran.FortranType; import claw.wani.language.ClawPragma; import claw.wani.language.ClawClause; @@ -82,6 +83,7 @@ public void transform(XcodeProgram xcodeml, Translator translator, // 1. Find the function/module declaration FfunctionDefinition fctDef = _claw.getPragma().findParentFunction(); + FmoduleDefinition modDef = _claw.getPragma().findParentModule(); for(String data : _claw.values(ClawClause.DATA)) { Xnode stmt = XnodeUtil.getFirstArrayAssign(_claw.getPragma(), data); @@ -101,9 +103,9 @@ public void transform(XcodeProgram xcodeml, Translator translator, } if(stmt != null && standardArrayRef) { - transformAssignStmt(xcodeml, fctDef, data, stmt, translator); + transformAssignStmt(xcodeml, fctDef, modDef, data, stmt, translator); } else { - transformData(xcodeml, fctDef, data, translator); + transformData(xcodeml, fctDef, modDef, data, translator); } } @@ -114,18 +116,20 @@ public void transform(XcodeProgram xcodeml, Translator translator, * Apply the transformation for the data list. * * @param xcodeml The XcodeML on which the transformations are applied. - * @param fctDef Function/module definition in which the data are nested. + * @param fctDef Function definition in which the data are nested. + * @param modDef Module definition in which the data are nested. * @param data Array identifier on which the caching is done. * @param translator Current instance of the translator. * @throws Exception If something prevent the transformation to be done. */ private void transformData(XcodeProgram xcodeml, FfunctionDefinition fctDef, + FmoduleDefinition modDef, String data, Translator translator) throws Exception { - List aRefs = checkOffsetAndGetArrayRefs(xcodeml, fctDef, data); + List aRefs = checkOffsetAndGetArrayRefs(xcodeml, fctDef, modDef, data); // Generate the cache variable and its assignment String type = aRefs.get(0).getType(); @@ -144,7 +148,8 @@ private void transformData(XcodeProgram xcodeml, FfunctionDefinition fctDef, * Apply the transformation for the LHS array reference. * * @param xcodeml The XcodeML on which the transformations are applied. - * @param fctDef Function/module definition in which the data are nested. + * @param fctDef Function definition in which the data are nested. + * @param modDef Module definition in which the data are nested. * @param data Array identifier on which the caching is done. * @param stmt First statement including the array ref on the lhs. * @param translator The translator used to applied the transformations. @@ -152,12 +157,13 @@ private void transformData(XcodeProgram xcodeml, FfunctionDefinition fctDef, */ private void transformAssignStmt(XcodeProgram xcodeml, FfunctionDefinition fctDef, + FmoduleDefinition modDef, String data, Xnode stmt, Translator translator) throws Exception { String type = stmt.matchDirectDescendant(Xcode.F_ARRAY_REF).getType(); - List aRefs = checkOffsetAndGetArrayRefs(xcodeml, fctDef, data); + List aRefs = checkOffsetAndGetArrayRefs(xcodeml, fctDef, modDef, data); Xnode cacheVar = generateCacheVarAndAssignStmt(xcodeml, data, type, fctDef, stmt, stmt); @@ -248,21 +254,27 @@ private String generateNameWithOffsetInfo(String basename, * @param xcodeml The current program * @param fctDef The function definition which holds the variable * information. + * @param modDef The module definition which which is used to check + * for the variable if not found in function. * @param var The variable on which the offset are inferred. * @return List of integer representing the offset for the given variable. * @throws IllegalTransformationException if symbol id is not found. */ private List generateInferredOffsets(XcodeProgram xcodeml, FfunctionDefinition fctDef, + FmoduleDefinition modDef, String var) throws IllegalTransformationException { Xid id = fctDef.getSymbolTable().get(var); if(id == null) { - throw new IllegalTransformationException("Variable " + var + - " defined in the data clause has not been found", - _claw.getPragma().lineNo() - ); + id = modDef.getSymbolTable().get(var); + if(id == null) { + throw new IllegalTransformationException("Variable " + var + + " defined in the data clause has not been found", + _claw.getPragma().lineNo() + ); + } } FbasicType basicType = xcodeml.getTypeTable().getBasicType(id); int dim = basicType.getDimensions(); @@ -361,12 +373,13 @@ private Xnode generateCacheVarAndAssignStmt(XcodeProgram xcodeml, String var, private List checkOffsetAndGetArrayRefs(XcodeProgram xcodeml, FfunctionDefinition fctDef, + FmoduleDefinition modDef, String var) throws IllegalTransformationException { List offsets = _claw.getOffsets(); if(offsets.isEmpty()) { - offsets = generateInferredOffsets(xcodeml, fctDef, var); + offsets = generateInferredOffsets(xcodeml, fctDef, modDef, var); } List arrayRefs = From 962b50687714d9145390f93648267f156b534d66 Mon Sep 17 00:00:00 2001 From: Remo Dietlicher Date: Fri, 2 Aug 2019 11:48:50 +0200 Subject: [PATCH 2/4] find parent module from fct node --- .../transformation/ll/caching/Kcaching.java | 26 ++++++------------- 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/cx2t/src/claw/wani/transformation/ll/caching/Kcaching.java b/cx2t/src/claw/wani/transformation/ll/caching/Kcaching.java index 849e2aa94..b8ab0c8f7 100644 --- a/cx2t/src/claw/wani/transformation/ll/caching/Kcaching.java +++ b/cx2t/src/claw/wani/transformation/ll/caching/Kcaching.java @@ -12,7 +12,6 @@ import claw.tatsu.xcodeml.xnode.common.*; import claw.tatsu.xcodeml.xnode.fortran.FbasicType; import claw.tatsu.xcodeml.xnode.fortran.FfunctionDefinition; -import claw.tatsu.xcodeml.xnode.fortran.FmoduleDefinition; import claw.tatsu.xcodeml.xnode.fortran.FortranType; import claw.wani.language.ClawPragma; import claw.wani.language.ClawClause; @@ -83,7 +82,6 @@ public void transform(XcodeProgram xcodeml, Translator translator, // 1. Find the function/module declaration FfunctionDefinition fctDef = _claw.getPragma().findParentFunction(); - FmoduleDefinition modDef = _claw.getPragma().findParentModule(); for(String data : _claw.values(ClawClause.DATA)) { Xnode stmt = XnodeUtil.getFirstArrayAssign(_claw.getPragma(), data); @@ -103,9 +101,9 @@ public void transform(XcodeProgram xcodeml, Translator translator, } if(stmt != null && standardArrayRef) { - transformAssignStmt(xcodeml, fctDef, modDef, data, stmt, translator); + transformAssignStmt(xcodeml, fctDef, data, stmt, translator); } else { - transformData(xcodeml, fctDef, modDef, data, translator); + transformData(xcodeml, fctDef, data, translator); } } @@ -116,20 +114,18 @@ public void transform(XcodeProgram xcodeml, Translator translator, * Apply the transformation for the data list. * * @param xcodeml The XcodeML on which the transformations are applied. - * @param fctDef Function definition in which the data are nested. - * @param modDef Module definition in which the data are nested. + * @param fctDef Function/module definition in which the data are nested. * @param data Array identifier on which the caching is done. * @param translator Current instance of the translator. * @throws Exception If something prevent the transformation to be done. */ private void transformData(XcodeProgram xcodeml, FfunctionDefinition fctDef, - FmoduleDefinition modDef, String data, Translator translator) throws Exception { - List aRefs = checkOffsetAndGetArrayRefs(xcodeml, fctDef, modDef, data); + List aRefs = checkOffsetAndGetArrayRefs(xcodeml, fctDef, data); // Generate the cache variable and its assignment String type = aRefs.get(0).getType(); @@ -148,8 +144,7 @@ private void transformData(XcodeProgram xcodeml, FfunctionDefinition fctDef, * Apply the transformation for the LHS array reference. * * @param xcodeml The XcodeML on which the transformations are applied. - * @param fctDef Function definition in which the data are nested. - * @param modDef Module definition in which the data are nested. + * @param fctDef Function/module definition in which the data are nested. * @param data Array identifier on which the caching is done. * @param stmt First statement including the array ref on the lhs. * @param translator The translator used to applied the transformations. @@ -157,13 +152,12 @@ private void transformData(XcodeProgram xcodeml, FfunctionDefinition fctDef, */ private void transformAssignStmt(XcodeProgram xcodeml, FfunctionDefinition fctDef, - FmoduleDefinition modDef, String data, Xnode stmt, Translator translator) throws Exception { String type = stmt.matchDirectDescendant(Xcode.F_ARRAY_REF).getType(); - List aRefs = checkOffsetAndGetArrayRefs(xcodeml, fctDef, modDef, data); + List aRefs = checkOffsetAndGetArrayRefs(xcodeml, fctDef, data); Xnode cacheVar = generateCacheVarAndAssignStmt(xcodeml, data, type, fctDef, stmt, stmt); @@ -254,21 +248,18 @@ private String generateNameWithOffsetInfo(String basename, * @param xcodeml The current program * @param fctDef The function definition which holds the variable * information. - * @param modDef The module definition which which is used to check - * for the variable if not found in function. * @param var The variable on which the offset are inferred. * @return List of integer representing the offset for the given variable. * @throws IllegalTransformationException if symbol id is not found. */ private List generateInferredOffsets(XcodeProgram xcodeml, FfunctionDefinition fctDef, - FmoduleDefinition modDef, String var) throws IllegalTransformationException { Xid id = fctDef.getSymbolTable().get(var); if(id == null) { - id = modDef.getSymbolTable().get(var); + id = fctDef.findParentModule().getSymbolTable().get(var); if(id == null) { throw new IllegalTransformationException("Variable " + var + " defined in the data clause has not been found", @@ -373,13 +364,12 @@ private Xnode generateCacheVarAndAssignStmt(XcodeProgram xcodeml, String var, private List checkOffsetAndGetArrayRefs(XcodeProgram xcodeml, FfunctionDefinition fctDef, - FmoduleDefinition modDef, String var) throws IllegalTransformationException { List offsets = _claw.getOffsets(); if(offsets.isEmpty()) { - offsets = generateInferredOffsets(xcodeml, fctDef, modDef, var); + offsets = generateInferredOffsets(xcodeml, fctDef, var); } List arrayRefs = From 42522aa1435ebaff110e14ddff9e129af7d444f2 Mon Sep 17 00:00:00 2001 From: Remo Dietlicher Date: Fri, 2 Aug 2019 11:59:39 +0200 Subject: [PATCH 3/4] test module vars --- test/claw/directive/kcache6/helper_module.f90 | 9 +++++ test/claw/directive/kcache6/main.f90 | 5 +++ test/claw/directive/kcache6/original_code.f90 | 40 +++++++++++++++++++ test/claw/directive/kcache6/reference.f90 | 37 +++++++++++++++++ 4 files changed, 91 insertions(+) create mode 100644 test/claw/directive/kcache6/helper_module.f90 create mode 100644 test/claw/directive/kcache6/main.f90 create mode 100644 test/claw/directive/kcache6/original_code.f90 create mode 100644 test/claw/directive/kcache6/reference.f90 diff --git a/test/claw/directive/kcache6/helper_module.f90 b/test/claw/directive/kcache6/helper_module.f90 new file mode 100644 index 000000000..98547862b --- /dev/null +++ b/test/claw/directive/kcache6/helper_module.f90 @@ -0,0 +1,9 @@ +module helper_module + implicit none + + public + real(KIND=8) :: array1(10,20) + real(KIND=8) :: array2(10,20) + real(KIND=8) :: array3(10,20) + +end module helper_module diff --git a/test/claw/directive/kcache6/main.f90 b/test/claw/directive/kcache6/main.f90 new file mode 100644 index 000000000..9d41f0b2f --- /dev/null +++ b/test/claw/directive/kcache6/main.f90 @@ -0,0 +1,5 @@ +PROGRAM claw_test + USE kcache_module, ONLY: kcache + + CALL kcache() +END PROGRAM claw_test diff --git a/test/claw/directive/kcache6/original_code.f90 b/test/claw/directive/kcache6/original_code.f90 new file mode 100644 index 000000000..ec30bdfdd --- /dev/null +++ b/test/claw/directive/kcache6/original_code.f90 @@ -0,0 +1,40 @@ +! +! This file is released under terms of BSD license +! See LICENSE file for more information +! +! Simple program to test the kcache directive +! + +MODULE kcache_module +USE helper_module, ONLY: array1, array2, array3 + +contains + +SUBROUTINE kcache() + + INTEGER :: i,j + + DO i = 1,10 + DO j = 1,20 + array1(i,j) = 1.0 + array2(i,j) = 2.0 + array3(i,j) = 3.0 + END DO + END DO + + DO i = 1,10 + DO j = 2,20 + !$claw kcache data(array2, array3) + !$claw kcache data(array1) offset(0,-1) + array1(i,j) = array1(i,j-1) * 2.0 + array2(i,j) = array2(i,j) * 2.0 + array1(i,j-1) + array3(i,j) = array3(i,j) * 2.0 + array1(i,j-1) + array2(i,j) + array1(i,j-1) = array2(i,j) + END DO + END DO + PRINT*, SUM(array1) + PRINT*, SUM(array2) + PRINT*, SUM(array3) +END SUBROUTINE kcache + +END MODULE kcache_module diff --git a/test/claw/directive/kcache6/reference.f90 b/test/claw/directive/kcache6/reference.f90 new file mode 100644 index 000000000..3e1f40ba6 --- /dev/null +++ b/test/claw/directive/kcache6/reference.f90 @@ -0,0 +1,37 @@ +MODULE kcache_module + USE helper_module , ONLY: array1 , array2 , array3 + +CONTAINS + SUBROUTINE kcache ( ) + + INTEGER :: i + INTEGER :: j + REAL ( KIND= 8 ) :: array2_k + REAL ( KIND= 8 ) :: array3_k + REAL ( KIND= 8 ) :: array1_k_m1 + + DO i = 1 , 10 , 1 + DO j = 1 , 20 , 1 + array1 ( i , j ) = 1.0 + array2 ( i , j ) = 2.0 + array3 ( i , j ) = 3.0 + END DO + END DO + DO i = 1 , 10 , 1 + DO j = 2 , 20 , 1 + array1_k_m1 = array1 ( i , j - 1 ) * 2.0 + array1 ( i , j ) = array1_k_m1 + array2_k = array2 ( i , j ) * 2.0 + array1_k_m1 + array2 ( i , j ) = array2_k + array3_k = array3 ( i , j ) * 2.0 + array1_k_m1 + array2_k + array3 ( i , j ) = array3_k + array1_k_m1 = array2_k + END DO + END DO + PRINT * , sum ( array1 ) + PRINT * , sum ( array2 ) + PRINT * , sum ( array3 ) + END SUBROUTINE kcache + +END MODULE kcache_module + From fa45fec2b50d6ee48149b817016fd10115ba2ae5 Mon Sep 17 00:00:00 2001 From: Remo Dietlicher Date: Mon, 5 Aug 2019 15:02:26 +0200 Subject: [PATCH 4/4] add license information --- test/claw/directive/kcache6/helper_module.f90 | 6 ++++++ test/claw/directive/kcache6/main.f90 | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/test/claw/directive/kcache6/helper_module.f90 b/test/claw/directive/kcache6/helper_module.f90 index 98547862b..fa6d9f16c 100644 --- a/test/claw/directive/kcache6/helper_module.f90 +++ b/test/claw/directive/kcache6/helper_module.f90 @@ -1,3 +1,9 @@ +! +! This file is released under terms of BSD license +! See LICENSE file for more information +! +! Helper module to provide some arrays that can be imported +! module helper_module implicit none diff --git a/test/claw/directive/kcache6/main.f90 b/test/claw/directive/kcache6/main.f90 index 9d41f0b2f..5a69ed978 100644 --- a/test/claw/directive/kcache6/main.f90 +++ b/test/claw/directive/kcache6/main.f90 @@ -1,3 +1,9 @@ +! +! This file is released under terms of BSD license +! See LICENSE file for more information +! +! Entry point for the module test +! PROGRAM claw_test USE kcache_module, ONLY: kcache