ilo: remove ilo_cbuf_state::count
[mesa.git] / src / gallium / drivers / radeon / radeon_llvm_util.c
index 3d30612ffd985c7da4cd56bc363118652b91f293..25be2459366b25ef63c6e8a8026f1c9569aa9ce4 100644 (file)
@@ -1,18 +1,46 @@
+/*
+ * Copyright 2012, 2013 Advanced Micro Devices, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ *
+ * Authors: Tom Stellard <thomas.stellard@amd.com>
+ *
+ */
+
 #include "radeon_llvm_util.h"
 #include "util/u_memory.h"
 
 #include <llvm-c/BitReader.h>
 #include <llvm-c/Core.h>
+#include <llvm-c/Transforms/PassManagerBuilder.h>
 
-static LLVMModuleRef radeon_llvm_parse_bitcode(const unsigned char * bitcode,
+LLVMModuleRef radeon_llvm_parse_bitcode(const unsigned char * bitcode,
                                                        unsigned bitcode_len)
 {
        LLVMMemoryBufferRef buf;
-       LLVMModuleRef module = LLVMModuleCreateWithName("radeon");
+       LLVMContextRef ctx = LLVMContextCreate();
+       LLVMModuleRef module;
 
        buf = LLVMCreateMemoryBufferWithMemoryRangeCopy((const char*)bitcode,
                                                        bitcode_len, "radeon");
-       LLVMParseBitcode(buf, &module, NULL);
+       LLVMParseBitcodeInContext(ctx, buf, &module, NULL);
        return module;
 }
 
@@ -23,6 +51,19 @@ unsigned radeon_llvm_get_num_kernels(const unsigned char *bitcode,
        return LLVMGetNamedMetadataNumOperands(mod, "opencl.kernels");
 }
 
+static void radeon_llvm_optimize(LLVMModuleRef mod)
+{
+       LLVMPassManagerBuilderRef builder = LLVMPassManagerBuilderCreate();
+       LLVMPassManagerRef pass_manager = LLVMCreatePassManager();
+
+       LLVMPassManagerBuilderUseInlinerWithThreshold(builder, 1000000000);
+       LLVMPassManagerBuilderPopulateModulePassManager(builder, pass_manager);
+
+       LLVMRunPassManager(pass_manager, mod);
+       LLVMPassManagerBuilderDispose(builder);
+       LLVMDisposePassManager(pass_manager);
+}
+
 LLVMModuleRef radeon_llvm_get_kernel_module(unsigned index,
                const unsigned char *bitcode, unsigned bitcode_len)
 {
@@ -45,5 +86,6 @@ LLVMModuleRef radeon_llvm_get_kernel_module(unsigned index,
                LLVMDeleteFunction(kernel_function);
        }
        FREE(kernel_metadata);
+       radeon_llvm_optimize(mod);
        return mod;
 }