|
| 1 | +/* |
| 2 | + * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * The Universal Permissive License (UPL), Version 1.0 |
| 6 | + * |
| 7 | + * Subject to the condition set forth below, permission is hereby granted to any |
| 8 | + * person obtaining a copy of this software, associated documentation and/or |
| 9 | + * data (collectively the "Software"), free of charge and under any and all |
| 10 | + * copyright rights in the Software, and any and all patent rights owned or |
| 11 | + * freely licensable by each licensor hereunder covering either (i) the |
| 12 | + * unmodified Software as contributed to or provided by such licensor, or (ii) |
| 13 | + * the Larger Works (as defined below), to deal in both |
| 14 | + * |
| 15 | + * (a) the Software, and |
| 16 | + * |
| 17 | + * (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if |
| 18 | + * one is included with the Software each a "Larger Work" to which the Software |
| 19 | + * is contributed by such licensors), |
| 20 | + * |
| 21 | + * without restriction, including without limitation the rights to copy, create |
| 22 | + * derivative works of, display, perform, and distribute the Software and make, |
| 23 | + * use, sell, offer for sale, import, export, have made, and have sold the |
| 24 | + * Software and the Larger Work(s), and to sublicense the foregoing rights on |
| 25 | + * either these or other terms. |
| 26 | + * |
| 27 | + * This license is subject to the following condition: |
| 28 | + * |
| 29 | + * The above copyright notice and either this complete permission notice or at a |
| 30 | + * minimum a reference to the UPL must be included in all copies or substantial |
| 31 | + * portions of the Software. |
| 32 | + * |
| 33 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 34 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 35 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 36 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 37 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 38 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 39 | + * SOFTWARE. |
| 40 | + */ |
| 41 | +package com.oracle.graal.python.shell; |
| 42 | + |
| 43 | +import java.io.IOException; |
| 44 | +import java.nio.file.Files; |
| 45 | +import java.nio.file.Path; |
| 46 | +import java.nio.file.Paths; |
| 47 | +import java.nio.file.StandardCopyOption; |
| 48 | +import java.nio.file.attribute.PosixFilePermission; |
| 49 | +import java.util.ArrayList; |
| 50 | +import java.util.Arrays; |
| 51 | +import java.util.HashSet; |
| 52 | +import java.util.List; |
| 53 | + |
| 54 | +public class GraalPythonCC extends GraalPythonCompiler { |
| 55 | + private String outputFilename; |
| 56 | + private boolean linkExecutable; |
| 57 | + private boolean link; |
| 58 | + private boolean linkLLI; |
| 59 | + private Boolean compile; |
| 60 | + private List<String> clangArgs; |
| 61 | + private List<String> execLinkArgs; |
| 62 | + private List<String> fileInputs; |
| 63 | + |
| 64 | + GraalPythonCC() { |
| 65 | + } |
| 66 | + |
| 67 | + private static List<String> execLinkPrefix = Arrays.asList(new String[]{ |
| 68 | + "clang", |
| 69 | + "-fembed-bitcode", |
| 70 | + "-fPIC", |
| 71 | + "-ggdb", |
| 72 | + "-O1", |
| 73 | + }); |
| 74 | + private static List<String> clangPrefix = Arrays.asList(new String[]{ |
| 75 | + "clang", |
| 76 | + "-emit-llvm", |
| 77 | + "-fPIC", |
| 78 | + "-Wno-int-to-void-pointer-cast", |
| 79 | + "-Wno-int-conversion", |
| 80 | + "-Wno-incompatible-pointer-types-discards-qualifiers", |
| 81 | + "-ggdb", |
| 82 | + "-O1", |
| 83 | + }); |
| 84 | + private static List<String> optPrefix = Arrays.asList(new String[]{ |
| 85 | + "opt", |
| 86 | + "-mem2reg", |
| 87 | + "-globalopt", |
| 88 | + "-simplifycfg", |
| 89 | + "-constprop", |
| 90 | + "-always-inline", |
| 91 | + "-instcombine", |
| 92 | + "-dse", |
| 93 | + "-loop-simplify", |
| 94 | + "-reassociate", |
| 95 | + "-licm", |
| 96 | + "-gvn", |
| 97 | + "-o", |
| 98 | + }); |
| 99 | + |
| 100 | + static void main(String[] args) { |
| 101 | + new GraalPythonCC().run(args); |
| 102 | + } |
| 103 | + |
| 104 | + private void run(String[] args) { |
| 105 | + parseOptions(args); |
| 106 | + launchCC(); |
| 107 | + } |
| 108 | + |
| 109 | + private void parseOptions(String[] args) { |
| 110 | + outputFilename = A_OUT; |
| 111 | + linkExecutable = true; |
| 112 | + linkLLI = false; |
| 113 | + link = true; |
| 114 | + verbose = false; |
| 115 | + compile = null; |
| 116 | + clangArgs = new ArrayList<>(clangPrefix); |
| 117 | + execLinkArgs = new ArrayList<>(execLinkPrefix); |
| 118 | + fileInputs = new ArrayList<>(); |
| 119 | + for (int i = 0; i < args.length; i++) { |
| 120 | + String arg = args[i]; |
| 121 | + switch (arg) { |
| 122 | + case "-o": |
| 123 | + clangArgs.add(arg); |
| 124 | + i++; |
| 125 | + if (i >= args.length) { |
| 126 | + throw new RuntimeException("-o needs an argument"); |
| 127 | + } |
| 128 | + outputFilename = arg = args[i]; |
| 129 | + break; |
| 130 | + case "-shared": |
| 131 | + linkExecutable = false; |
| 132 | + break; |
| 133 | + case "-c": |
| 134 | + link = false; |
| 135 | + linkExecutable = false; |
| 136 | + break; |
| 137 | + case "--link-lli-scripts": |
| 138 | + linkLLI = true; |
| 139 | + continue; // skip adding this to clang args |
| 140 | + case "-v": |
| 141 | + if (!verbose) { |
| 142 | + verbose = true; |
| 143 | + continue; // the first verbose is not passed on to clang |
| 144 | + } |
| 145 | + break; |
| 146 | + default: |
| 147 | + if (arg.endsWith(".o") || arg.endsWith(".bc")) { |
| 148 | + if (compile == null) { |
| 149 | + compile = false; |
| 150 | + } else if (compile != false) { |
| 151 | + throw new RuntimeException("cannot mix source and compiled sources"); |
| 152 | + } |
| 153 | + fileInputs.add(arg); |
| 154 | + } else if (arg.endsWith(".c") || arg.endsWith(".cpp") || arg.endsWith(".cxx")) { |
| 155 | + if (compile == null) { |
| 156 | + compile = true; |
| 157 | + } else if (compile != true) { |
| 158 | + throw new RuntimeException("cannot mix source and compiled sources"); |
| 159 | + } |
| 160 | + fileInputs.add(arg); |
| 161 | + } else { |
| 162 | + execLinkArgs.add(arg); |
| 163 | + } |
| 164 | + } |
| 165 | + clangArgs.add(arg); |
| 166 | + } |
| 167 | + String targetFlags = System.getenv("LLVM_TARGET_FLAGS"); |
| 168 | + if (targetFlags != null) { |
| 169 | + clangArgs.addAll(Arrays.asList(targetFlags.split(" "))); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + private void launchCC() { |
| 174 | + // run the clang compiler to generate bc files |
| 175 | + try { |
| 176 | + Files.delete(Paths.get(outputFilename)); |
| 177 | + } catch (IOException e) { |
| 178 | + // no matter; |
| 179 | + } |
| 180 | + |
| 181 | + if (compile) { |
| 182 | + exec(clangArgs); |
| 183 | + logV("opt: ", fileInputs); |
| 184 | + if (!Files.exists(Paths.get(outputFilename))) { |
| 185 | + // if no explicit output filename was given or produced, we search the commandline |
| 186 | + // for files for which now have a bc file and optimize those. This happens when you |
| 187 | + // pass multiple .c files to clang and ask it to emit llvm bitcode |
| 188 | + for (String f : fileInputs) { |
| 189 | + String bcFile = bcFileFromFilename(f); |
| 190 | + assert Files.exists(Paths.get(bcFile)); |
| 191 | + optFile(bcFile); |
| 192 | + try { |
| 193 | + String objFile = objectFileFromFilename(f); |
| 194 | + logV("Optimized:", bcFile, "->", objFile); |
| 195 | + Files.move(Paths.get(bcFile), Paths.get(objFile)); |
| 196 | + } catch (IOException e) { |
| 197 | + throw new RuntimeException(e); |
| 198 | + } |
| 199 | + } |
| 200 | + } else { |
| 201 | + optFile(outputFilename); |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + if (link) { |
| 206 | + linkShared(fileInputs); |
| 207 | + if (linkExecutable) { |
| 208 | + try { |
| 209 | + Path linkedBCfile = Files.move(Paths.get(outputFilename), Paths.get(bcFileFromFilename(outputFilename)), StandardCopyOption.REPLACE_EXISTING); |
| 210 | + linkExecutable(outputFilename, linkedBCfile.toString()); |
| 211 | + } catch (IOException e) { |
| 212 | + throw new RuntimeException(e); |
| 213 | + } |
| 214 | + } |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + private void linkExecutable(String executableScript, String linkedBcFile) throws IOException { |
| 219 | + if (linkLLI) { |
| 220 | + List<String> cmdline = GraalPythonMain.getCmdline(Arrays.asList(), Arrays.asList()); |
| 221 | + cmdline.add("-LLI"); |
| 222 | + cmdline.add(linkedBcFile); |
| 223 | + Path executablePath = Paths.get(executableScript); |
| 224 | + Files.write(executablePath, String.join(" ", cmdline).getBytes()); |
| 225 | + HashSet<PosixFilePermission> perms = new HashSet<>(Arrays.asList(new PosixFilePermission[]{PosixFilePermission.OWNER_EXECUTE, PosixFilePermission.GROUP_EXECUTE})); |
| 226 | + perms.addAll(Files.getPosixFilePermissions(executablePath)); |
| 227 | + } else { |
| 228 | + execLinkArgs.add(linkedBcFile); |
| 229 | + execLinkArgs.add("-o"); |
| 230 | + execLinkArgs.add(executableScript); |
| 231 | + exec(execLinkArgs); |
| 232 | + } |
| 233 | + } |
| 234 | + |
| 235 | + private void linkShared(List<String> bitcodeFiles) { |
| 236 | + if (fileInputs.size() > 0) { |
| 237 | + ArrayList<String> ldCmd = new ArrayList<>(bitcodeFiles); |
| 238 | + if (verbose) { |
| 239 | + ldCmd.add("-v"); |
| 240 | + } |
| 241 | + ldCmd.add("-o"); |
| 242 | + ldCmd.add(outputFilename); |
| 243 | + GraalPythonLD.main(ldCmd.toArray(new String[0])); |
| 244 | + } |
| 245 | + } |
| 246 | + |
| 247 | + private void optFile(String bcFile) { |
| 248 | + List<String> opt = new ArrayList<>(optPrefix); |
| 249 | + opt.add(bcFile); |
| 250 | + opt.add(bcFile); |
| 251 | + exec(opt); |
| 252 | + } |
| 253 | + |
| 254 | + private static String bcFileFromFilename(String f) { |
| 255 | + int dotIdx = f.lastIndexOf('.'); |
| 256 | + if (dotIdx > 1) { |
| 257 | + return f.substring(0, dotIdx + 1) + "bc"; |
| 258 | + } else { |
| 259 | + return f + ".bc"; |
| 260 | + } |
| 261 | + } |
| 262 | + |
| 263 | + private static String objectFileFromFilename(String f) { |
| 264 | + return bcFileFromFilename(f).replaceAll("\\.bc$", ".o"); |
| 265 | + } |
| 266 | +} |
0 commit comments