1 /*
   2  * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package com.oracle.graal.compiler.ptx.test;
  24 
  25 import org.junit.*;
  26 
  27 import com.oracle.graal.api.code.*;
  28 import com.oracle.graal.api.meta.*;
  29 import com.oracle.graal.api.runtime.*;
  30 import com.oracle.graal.compiler.*;
  31 import com.oracle.graal.compiler.ptx.*;
  32 import com.oracle.graal.compiler.test.*;
  33 import com.oracle.graal.debug.*;
  34 import com.oracle.graal.java.*;
  35 import com.oracle.graal.nodes.*;
  36 import com.oracle.graal.nodes.type.*;
  37 import com.oracle.graal.phases.*;
  38 import com.oracle.graal.phases.PhasePlan.*;
  39 import com.oracle.graal.ptx.*;
  40 
  41 /**
  42  * Test class for small Java methods compiled to PTX kernels.
  43  */
  44 public class BasicPTXTest extends GraalCompilerTest {
  45 
  46     @Test
  47     public void testAdd() {
  48         test("testAddSnippet");
  49     }
  50 
  51     public static int testAddSnippet(int a) {
  52         return a + 1;
  53     }
  54 
  55     @Test
  56     public void testArray() {
  57         test("testArraySnippet");
  58     }
  59 
  60     public static int testArraySnippet(int[] array) {
  61         return array[0];
  62     }
  63 
  64     private CompilationResult test(String snippet) {
  65         StructuredGraph graph = parse(snippet);
  66         Debug.dump(graph, "Graph");
  67         TargetDescription target = new TargetDescription(new PTX(), true, 1, 0, true);
  68         PTXBackend ptxBackend = new PTXBackend(Graal.getRequiredCapability(CodeCacheProvider.class), target);
  69         PhasePlan phasePlan = new PhasePlan();
  70         GraphBuilderPhase graphBuilderPhase = new GraphBuilderPhase(runtime, GraphBuilderConfiguration.getDefault(), OptimisticOptimizations.NONE);
  71         phasePlan.addPhase(PhasePosition.AFTER_PARSING, graphBuilderPhase);
  72         phasePlan.addPhase(PhasePosition.AFTER_PARSING, new PTXPhase());
  73         new PTXPhase().apply(graph);
  74         CompilationResult result = GraalCompiler.compileMethod(runtime, ptxBackend, target, graph.method(), graph, null, phasePlan, OptimisticOptimizations.NONE, new SpeculationLog());
  75         return result;
  76     }
  77 
  78     private static class PTXPhase extends Phase {
  79 
  80         @Override
  81         protected void run(StructuredGraph graph) {
  82             for (LocalNode local : graph.getNodes(LocalNode.class)) {
  83                 if (local.kind() == Kind.Object) {
  84                     local.setStamp(StampFactory.declaredNonNull(local.objectStamp().type()));
  85                 }
  86             }
  87         }
  88 
  89     }
  90 
  91     public static void main(String[] args) {
  92         BasicPTXTest basicPTXTest = new BasicPTXTest();
  93         System.out.println("testAddSnippet: \n" + new String(basicPTXTest.test("testAddSnippet").getTargetCode()));
  94         System.out.println("testArraySnippet: \n" + new String(basicPTXTest.test("testArraySnippet").getTargetCode()));
  95     }
  96 }