xref: /aosp_15_r20/external/mesa3d/src/gallium/auxiliary/gallivm/lp_bld_init.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /**************************************************************************
2  *
3  * Copyright 2009 VMware, Inc.
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21  * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  **************************************************************************/
27 
28 
29 #ifndef LP_BLD_INIT_H
30 #define LP_BLD_INIT_H
31 
32 
33 #include "util/compiler.h"
34 #include "util/u_pointer.h" // for func_pointer
35 #include "util/u_cpu_detect.h"
36 #include "lp_bld.h"
37 #include "lp_bld_passmgr.h"
38 
39 #if GALLIVM_USE_ORCJIT
40 #include <llvm-c/Orc.h>
41 #else
42 #include <llvm-c/ExecutionEngine.h>
43 #endif
44 
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48 
49 struct lp_cached_code;
50 struct gallivm_state
51 {
52    char *module_name;
53    LLVMModuleRef module;
54    LLVMTargetDataRef target;
55 #if GALLIVM_USE_ORCJIT
56    /* own this->module */
57    LLVMOrcThreadSafeContextRef _ts_context;
58    /* each module is in its own jitdylib */
59    LLVMOrcJITDylibRef _per_module_jd;
60 #else
61    LLVMExecutionEngineRef engine;
62    struct lp_passmgr *passmgr;
63    LLVMMCJITMemoryManagerRef memorymgr;
64    struct lp_generated_code *code;
65 #endif
66    LLVMContextRef context;
67    LLVMBuilderRef builder;
68    struct lp_cached_code *cache;
69    unsigned compiled;
70    LLVMValueRef coro_malloc_hook;
71    LLVMValueRef coro_free_hook;
72    LLVMValueRef debug_printf_hook;
73 
74    LLVMTypeRef coro_malloc_hook_type;
75    LLVMTypeRef coro_free_hook_type;
76 
77    LLVMValueRef get_time_hook;
78 
79    LLVMValueRef texture_descriptor;
80    LLVMValueRef sampler_descriptor;
81 };
82 
83 unsigned
84 lp_build_init_native_width(void);
85 
86 bool
87 lp_build_init(void);
88 
89 
90 struct gallivm_state *
91 gallivm_create(const char *name, lp_context_ref *context,
92                struct lp_cached_code *cache);
93 
94 void
95 gallivm_destroy(struct gallivm_state *gallivm);
96 
97 void
98 gallivm_free_ir(struct gallivm_state *gallivm);
99 
100 void
101 gallivm_verify_function(struct gallivm_state *gallivm,
102                         LLVMValueRef func);
103 
104 void
105 gallivm_add_global_mapping(struct gallivm_state *gallivm, LLVMValueRef sym, void* addr);
106 
107 /**
108  * for ORCJIT, after this function gets called, all access and modification to
109  * module and any structure associated to it should be avoided,
110  * as module has been moved into ORCJIT and may be recycled
111  */
112 void
113 gallivm_compile_module(struct gallivm_state *gallivm);
114 
115 func_pointer
116 gallivm_jit_function(struct gallivm_state *gallivm,
117                      LLVMValueRef func, const char *func_name);
118 
119 void
120 gallivm_stub_func(struct gallivm_state *gallivm, LLVMValueRef func);
121 
122 unsigned gallivm_get_perf_flags(void);
123 
124 void lp_init_clock_hook(struct gallivm_state *gallivm);
125 
126 void lp_init_env_options(void);
127 
128 static inline void
lp_bld_ppc_disable_denorms(void)129 lp_bld_ppc_disable_denorms(void)
130 {
131 #if DETECT_ARCH_PPC_64
132    /* Set the NJ bit in VSCR to 0 so denormalized values are handled as
133     * specified by IEEE standard (PowerISA 2.06 - Section 6.3). This guarantees
134     * that some rounding and half-float to float handling does not round
135     * incorrectly to 0.
136     * XXX: should eventually follow same logic on all platforms.
137     * Right now denorms get explicitly disabled (but elsewhere) for x86,
138     * whereas ppc64 explicitly enables them...
139     */
140    if (util_get_cpu_caps()->has_altivec) {
141       unsigned short mask[] = { 0xFFFF, 0xFFFF, 0xFFFF, 0xFFFF,
142                                 0xFFFF, 0xFFFF, 0xFFFE, 0xFFFF };
143       __asm (
144         "mfvscr %%v1\n"
145         "vand   %0,%%v1,%0\n"
146         "mtvscr %0"
147         :
148         : "r" (*mask)
149       );
150    }
151 #endif
152 }
153 
154 #ifdef __cplusplus
155 }
156 #endif
157 
158 #endif /* !LP_BLD_INIT_H */
159