aboutsummaryrefslogtreecommitdiff
path: root/src/core/compiler.cpp
blob: bc7dd186eef41335e9d2baf71f08c82fe278e7a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*
 * Copyright (c) 2011, Denis Steckelmacher <steckdenis@yahoo.fr>
 * Copyright (c) 2012-2014, Texas Instruments Incorporated - http://www.ti.com/
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the copyright holder nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/**
 * \file compiler.cpp
 * \brief Compiler wrapper around Clang
 */

#include "compiler.h"
#include "deviceinterface.h"

#include <cstring>
#include <cstdio>
#include <string>
#include <sstream>
#include <iostream>
#include <clang/Frontend/CompilerInvocation.h>
#include <clang/Frontend/TextDiagnosticPrinter.h>
#include <clang/Frontend/LangStandard.h>
#include <clang/Basic/Diagnostic.h>
#include <clang/CodeGen/CodeGenAction.h>
#include <llvm/ADT/SmallVector.h>
#include <llvm/Support/Host.h>
#include <llvm/Support/MemoryBuffer.h>
#include <llvm/IR/Module.h>
#include <llvm/IR/LLVMContext.h>
#include <sys/stat.h>

std::string get_ocl_dsp();

using namespace Coal;

Compiler::Compiler(DeviceInterface *device)
: p_device(device), p_module(0), p_optimize(true), p_log_stream(p_log),
  p_log_printer(0)
{
}

Compiler::~Compiler()
{

}

int Compiler::compile(const std::string &options,
                           llvm::MemoryBuffer *source)
{
    /* Set options */
    p_options = options;

    clang::CodeGenOptions &codegen_opts = p_compiler.getCodeGenOpts();
    clang::DiagnosticOptions &diag_opts = p_compiler.getDiagnosticOpts();
    clang::FrontendOptions &frontend_opts = p_compiler.getFrontendOpts();
    clang::HeaderSearchOptions &header_opts = p_compiler.getHeaderSearchOpts();
    clang::LangOptions &lang_opts = p_compiler.getLangOpts();
    clang::TargetOptions &target_opts = p_compiler.getTargetOpts();
    clang::PreprocessorOptions &prep_opts = p_compiler.getPreprocessorOpts();
    clang::CompilerInvocation &invocation = p_compiler.getInvocation();

    // Set codegen options
    codegen_opts.setDebugInfo(clang::CodeGenOptions::NoDebugInfo);
    codegen_opts.AsmVerbose = true;
    codegen_opts.CodeModel = "default";
    codegen_opts.ThreadModel = "posix";

    // level 3 is too much for the pocl transformations.
    codegen_opts.OptimizationLevel = 2;

    // Set diagnostic options
    diag_opts.Pedantic = true;
    diag_opts.ShowColumn = true;
    diag_opts.ShowLocation = true;
    diag_opts.ShowCarets = false;
    diag_opts.ShowFixits = true;
    diag_opts.ShowColors = false;
    diag_opts.ErrorLimit = 19;
    diag_opts.MessageLength = 0;

    // Set frontend options
    frontend_opts.ProgramAction = clang::frontend::EmitLLVMOnly;
    frontend_opts.DisableFree = true;

    // Set header search options
    header_opts.Verbose = false;
    header_opts.UseBuiltinIncludes = false;
    header_opts.UseStandardSystemIncludes = false;
    header_opts.UseStandardCXXIncludes = false;

    // Set preprocessor options
    prep_opts.RetainRemappedFileBuffers = true;
    //prep_opts.ImplicitPCHInclude = "/usr/share/ti/opencl/clc.h";
    prep_opts.Includes.push_back("clc.h");
    prep_opts.Includes.push_back(p_device->builtinsHeader());

    // Set lang options
    lang_opts.NoBuiltin = true;
    lang_opts.OpenCL = true;
    lang_opts.CPlusPlus = false;

    // Set target options
    cl_device_type devtype;
    p_device->info(CL_DEVICE_TYPE, sizeof(devtype), &devtype, 0);

    if (devtype == CL_DEVICE_TYPE_CPU) {
       // Originally: target_opts.Triple = llvm::sys::getHostTriple();
       target_opts.Triple = llvm::sys::getDefaultTargetTriple();
       // For CPU, we need OpenCL address space modifiers on our data types.
       // This setting achieves that, and shamrock needs to know clang's address mapping.
       // In the future, if we ever get a SPIR->ARM backend, this may be unnecessary.
       lang_opts.FakeAddressSpaceMap = true;
    }
    else // devtype != CL_DEVICE_TYPE_CPU
    {
       // For 6X, use the 'spir' target, since it implements opencl specs
       target_opts.Triple = "spir-unknown-unknown-unknown";

       // Currently, llp6x does not handle fused multiply and add
       // llvm intrinsics (llvm.fmuladd.*). Disable generating these
       // intrinsics using clang -ffp-contract=off option
       codegen_opts.setFPContractMode(clang::CodeGenOptions::FPC_Off);
    }

    // Parse the user options
    std::istringstream options_stream(options);
    std::string token;
    bool Werror = false, inI = false, inD = false;

#ifndef SHAMROCK_BUILD
    // Add opencl-headers' package default install include path as location to search
    std::string header_path(get_ocl_dsp());
#else  // TODO: /usr/include/CL is where opencl headers go, but use ENV vars?
    std::string header_path("/usr/include/CL");
#endif
    header_opts.AddPath(header_path, clang::frontend::Angled, false, false);


    while (options_stream >> token)
    {
        if (inI)
        {
            // token is an include path
            header_opts.AddPath(token, clang::frontend::Angled, false, false);
            inI = false;
            continue;
        }
        else if (inD)
        {
            // token is name or name=value
            prep_opts.addMacroDef(token);
            inD = false;
			continue;
        }

	//Handle -I xxx or -Ixxx. Assuming no other -I option prefix
        if (token == "-I")
        {
            inI = true;
        }
	else if (token.compare(0,2,"-I") == 0) 
	{
	   header_opts.AddPath(token.substr(2), clang::frontend::Angled, false,
                                                                         false);
	}
	//Handle -D xxx or -Dxxx. Assuming no other -D option prefix
        else if (token == "-D")
        {
            inD = true;
        }
	else if (token.compare(0,2,"-D") == 0) //Handle -Dxxx (no space between)
	{
	   prep_opts.addMacroDef(token.substr(2));
	}
        else if (token == "-cl-single-precision-constant")
        {
            lang_opts.SinglePrecisionConstants = true;
        }
        else if (token == "-cl-opt-disable")
        {
            p_optimize = false;
            codegen_opts.OptimizationLevel = 0;
        }
        else if (token == "-cl-mad-enable")
        {
            codegen_opts.LessPreciseFPMAD = true;
        }
        else if (token == "-cl-unsafe-math-optimizations")
        {
            codegen_opts.UnsafeFPMath = true;
        }
        else if (token == "-cl-finite-math-only")
        {
            codegen_opts.NoInfsFPMath = true;
            codegen_opts.NoNaNsFPMath = true;
        }
        else if (token == "-cl-fast-relaxed-math")
        {
            codegen_opts.UnsafeFPMath = true;
            codegen_opts.NoInfsFPMath = true;
            codegen_opts.NoNaNsFPMath = true;
            lang_opts.FastRelaxedMath = true;
        }
        else if (token == "-cl-denorms-are-zero")
        {
            //noop: there seems to be no corresponding clang option.
        }
        else if (token == "-cl-strict-aliasing")
        {
            codegen_opts.StructPathTBAA = false; // inverse of -fno-strict-aliasing
        }
        else if (token == "-cl-no-signed-zeros")
        {
            codegen_opts.NoSignedZeros = true;
        }
        else if (token == "-w")
        {
            diag_opts.IgnoreWarnings = true;
        }
        else if (token == "-Werror")
        {
            Werror = true;
        }
        else if (token == "-cl-std=CL1.1")
        {
        }
        else if (token == "-cl-kernel-arg-info")
        {
            // required by clGetKernelArgInfo() v1.2 API
            codegen_opts.EmitOpenCLArgMetadata = true;
        }
        else
        {
            return CL_INVALID_BUILD_OPTIONS;
        }
    }

    add_macrodefs_for_supported_opencl_extensions(prep_opts);  

    // Set invocation options
    //invocation.setLangDefaults(lang_opts,clang::IK_OpenCL);
    invocation.setLangDefaults(lang_opts,clang::IK_OpenCL, clang::LangStandard::lang_opencl12);

    // Create the diagnostics engine
    p_log_printer = new clang::TextDiagnosticPrinter(p_log_stream, &diag_opts);
    p_compiler.createDiagnostics(p_log_printer);

    if (!p_compiler.hasDiagnostics())
        return false;

    p_compiler.getDiagnostics().setWarningsAsErrors(Werror);

    // Feed the compiler with source
    frontend_opts.Inputs.push_back(clang::FrontendInputFile("program.cl", clang::IK_OpenCL));

    //ASW  TODO cleanup
#if 0
    prep_opts.addRemappedFile("program.cl", source);
#else

    const llvm::StringRef s_data(source->getBuffer());
    const llvm::StringRef s_name("<source>");
    std::unique_ptr<llvm::MemoryBuffer> buffer =
    llvm::MemoryBuffer::getMemBuffer(s_data, s_name);

    prep_opts.addRemappedFile("program.cl", buffer.get());
#endif

    //timespec t0, t1;
    //clock_gettime(CLOCK_MONOTONIC, &t0);
    // Compile

    clang::CodeGenAction *Act = new clang::EmitLLVMOnlyAction(&llvm::getGlobalContext());
    if (!p_compiler.ExecuteAction(*Act))
    {
        // DEBUG
        std::cout << log() << std::endl;
        return true;
    }

    //clock_gettime(CLOCK_MONOTONIC, &t1);
    //printf("clang time: %6.4f secs\n",
    //(float)t1.tv_sec-t0.tv_sec+(t1.tv_nsec-t0.tv_nsec)/1e9);

    p_log_stream.flush();
    p_module = Act->takeModule().release();

    // uncomment to debug the llvm IR
    // p_module->dump();

    return false;
}

// Query the device to get list of supported OpenCL extensions.  Standard
// requires that each supported extension has a macro definition with the
// same name as the extension
void Compiler::add_macrodefs_for_supported_opencl_extensions
                                        (clang::PreprocessorOptions &prep_opts)
{
    // Get the extensions string for the device
    size_t size;
    p_device->info(CL_DEVICE_EXTENSIONS, 0, NULL, &size);

    char *extensions = new char[size + 1];
    memset( extensions, CHAR_MIN, sizeof(char)*(size+1) );

    p_device->info(CL_DEVICE_EXTENSIONS, sizeof(char)*size, extensions, NULL);

    // Create macro definitions from the extension names
    std::istringstream extensions_stream(extensions);
    std::string token;

    while (extensions_stream >> token)
       prep_opts.addMacroDef(token);

    delete [] extensions;
}

const std::string &Compiler::log() const
{
    return p_log;
}

const std::string &Compiler::options() const
{
    return p_options;
}

bool Compiler::optimize() const
{
    return p_optimize;
}

llvm::Module *Compiler::module() const
{
    return p_module;
}

void Compiler::appendLog(const std::string &log)
{
    p_log += log;
}