xref: /aosp_15_r20/external/mesa3d/src/amd/compiler/tests/helpers.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2020 Valve Corporation
3  *
4  * SPDX-License-Identifier: MIT
5  */
6 #ifndef ACO_TEST_HELPERS_H
7 #define ACO_TEST_HELPERS_H
8 
9 #include "vulkan/vulkan.h"
10 
11 #include "framework.h"
12 #include "ac_gpu_info.h"
13 #include "nir_builder.h"
14 #include <functional>
15 
16 enum QoShaderDeclType {
17    QoShaderDeclType_ubo,
18    QoShaderDeclType_ssbo,
19    QoShaderDeclType_img_buf,
20    QoShaderDeclType_img,
21    QoShaderDeclType_tex_buf,
22    QoShaderDeclType_combined,
23    QoShaderDeclType_tex,
24    QoShaderDeclType_samp,
25    QoShaderDeclType_in,
26    QoShaderDeclType_out,
27 };
28 
29 struct QoShaderDecl {
30    const char* name;
31    const char* type;
32    QoShaderDeclType decl_type;
33    // TODO: array size?
34    unsigned location;
35    unsigned component;
36    unsigned binding;
37    unsigned set;
38 };
39 
40 struct QoShaderModuleCreateInfo {
41    void* pNext;
42    size_t spirvSize;
43    const void* pSpirv;
44    uint32_t declarationCount;
45    const QoShaderDecl* pDeclarations;
46    VkShaderStageFlagBits stage;
47 };
48 
49 extern ac_shader_config config;
50 extern aco_shader_info info;
51 extern std::unique_ptr<aco::Program> program;
52 extern aco::Builder bld;
53 extern aco::Temp inputs[16];
54 extern nir_builder *nb;
55 
56 namespace aco {
57 struct ra_test_policy;
58 }
59 
60 void create_program(enum amd_gfx_level gfx_level, aco::Stage stage, unsigned wave_size = 64,
61                     enum radeon_family family = CHIP_UNKNOWN);
62 bool setup_cs(const char* input_spec, enum amd_gfx_level gfx_level,
63               enum radeon_family family = CHIP_UNKNOWN, const char* subvariant = "",
64               unsigned wave_size = 64);
65 bool
66 setup_nir_cs(enum amd_gfx_level gfx_level, gl_shader_stage stage = MESA_SHADER_COMPUTE,
67              enum radeon_family family = CHIP_UNKNOWN, const char* subvariant = "");
68 
69 void finish_program(aco::Program* program, bool endpgm = true, bool dominance = false);
70 void finish_validator_test();
71 void finish_opt_test();
72 void finish_setup_reduce_temp_test();
73 void finish_lower_subdword_test();
74 void finish_ra_test(aco::ra_test_policy);
75 void finish_optimizer_postRA_test();
76 void finish_to_hw_instr_test();
77 void finish_schedule_vopd_test();
78 void finish_waitcnt_test();
79 void finish_insert_nops_test(bool endpgm = true);
80 void finish_form_hard_clause_test();
81 void finish_assembler_test();
82 void finish_isel_test(enum ac_hw_stage hw_stage = AC_HW_COMPUTE_SHADER, unsigned wave_size = 64);
83 
84 void writeout(unsigned i, aco::Temp tmp = aco::Temp(0, aco::s1));
85 void writeout(unsigned i, aco::Builder::Result res);
86 void writeout(unsigned i, aco::Operand op);
87 void writeout(unsigned i, aco::Operand op0, aco::Operand op1);
88 
89 aco::Temp fneg(aco::Temp src, aco::Builder b = bld);
90 aco::Temp fabs(aco::Temp src, aco::Builder b = bld);
91 aco::Temp f2f32(aco::Temp src, aco::Builder b = bld);
92 aco::Temp f2f16(aco::Temp src, aco::Builder b = bld);
93 aco::Temp u2u16(aco::Temp src, aco::Builder b = bld);
94 aco::Temp fadd(aco::Temp src0, aco::Temp src1, aco::Builder b = bld);
95 aco::Temp fmul(aco::Temp src0, aco::Temp src1, aco::Builder b = bld);
96 aco::Temp fma(aco::Temp src0, aco::Temp src1, aco::Temp src2, aco::Builder b = bld);
97 aco::Temp fsat(aco::Temp src, aco::Builder b = bld);
98 aco::Temp fmin(aco::Temp src0, aco::Temp src1, aco::Builder b = bld);
99 aco::Temp fmax(aco::Temp src0, aco::Temp src1, aco::Builder b = bld);
100 aco::Temp ext_ushort(aco::Temp src, unsigned idx, aco::Builder b = bld);
101 aco::Temp ext_ubyte(aco::Temp src, unsigned idx, aco::Builder b = bld);
102 void emit_divergent_if_else(aco::Program* prog, aco::Builder& b, aco::Operand cond,
103                             std::function<void()> then, std::function<void()> els);
104 
105 /* vulkan helpers */
106 VkDevice get_vk_device(enum amd_gfx_level gfx_level);
107 VkDevice get_vk_device(enum radeon_family family);
108 
109 void print_pipeline_ir(VkDevice device, VkPipeline pipeline, VkShaderStageFlagBits stages,
110                        const char* name, bool remove_encoding = false);
111 
112 VkShaderModule __qoCreateShaderModule(VkDevice dev, const QoShaderModuleCreateInfo* info);
113 
114 class PipelineBuilder {
115 public:
116    /* inputs */
117    VkDevice device;
118    VkFormat color_outputs[16];
119    VkFormat ds_output;
120    VkPrimitiveTopology topology;
121    VkSampleCountFlagBits samples;
122    bool sample_shading_enable;
123    float min_sample_shading;
124    uint32_t patch_size;
125    VkPipelineVertexInputStateCreateInfo vs_input;
126    VkVertexInputBindingDescription vs_bindings[16];
127    VkVertexInputAttributeDescription vs_attributes[16];
128    VkPushConstantRange push_constant_range;
129    uint64_t desc_layouts_used;
130    unsigned num_desc_bindings[64];
131    VkDescriptorSetLayoutBinding desc_bindings[64][64];
132    VkPipelineShaderStageCreateInfo stages[5];
133    VkShaderStageFlags owned_stages;
134 
135    /* outputs */
136    VkGraphicsPipelineCreateInfo gfx_pipeline_info;
137    VkComputePipelineCreateInfo cs_pipeline_info;
138    VkDescriptorSetLayout desc_layouts[64];
139    VkPipelineLayout pipeline_layout;
140    VkRenderPass render_pass;
141    VkPipeline pipeline;
142 
143    PipelineBuilder(VkDevice dev);
144    ~PipelineBuilder();
145 
146    PipelineBuilder(const PipelineBuilder&) = delete;
147    PipelineBuilder& operator=(const PipelineBuilder&) = delete;
148 
149    void add_desc_binding(VkShaderStageFlags stage_flags, uint32_t layout, uint32_t binding,
150                          VkDescriptorType type, uint32_t count = 1);
151 
152    void add_vertex_binding(uint32_t binding, uint32_t stride,
153                            VkVertexInputRate rate = VK_VERTEX_INPUT_RATE_VERTEX);
154    void add_vertex_attribute(uint32_t location, uint32_t binding, VkFormat format, uint32_t offset);
155 
156    void add_resource_decls(QoShaderModuleCreateInfo* module);
157    void add_io_decls(QoShaderModuleCreateInfo* module);
158 
159    void add_stage(VkShaderStageFlagBits stage, VkShaderModule module, const char* name = "main");
160    void add_stage(VkShaderStageFlagBits stage, QoShaderModuleCreateInfo module,
161                   const char* name = "main");
162    void add_vsfs(VkShaderModule vs, VkShaderModule fs);
163    void add_vsfs(QoShaderModuleCreateInfo vs, QoShaderModuleCreateInfo fs);
164    void add_cs(VkShaderModule cs);
165    void add_cs(QoShaderModuleCreateInfo cs);
166 
167    bool is_compute();
168 
169    void create_pipeline();
170 
171    void print_ir(VkShaderStageFlagBits stages, const char* name, bool remove_encoding = false);
172 
173 private:
174    void create_compute_pipeline();
175    void create_graphics_pipeline();
176 };
177 
178 #endif /* ACO_TEST_HELPERS_H */
179