Compiler "Optimizes" Out 对象初始化函数

Compiler "Optimizes" Out Object Initialization Function

我有一个对象需要在使用之前填充,名为 pipelineInfo。为了填充对象,我使用了一个名为 createPipelineInfo 的函数。当我使用 visual studio 编译调试版本时,这非常有效,但是当我尝试编译发布版本时,编译器“优化”了整个 createPipelineInfo 函数。

这里是初始化对象的调用及其使用:

VkGraphicsPipelineCreateInfo pipelineInfo = createPipelineInfo(shaderStages, vertexInputInfo, inputAssembly, viewportState, rasterizer,
    multisampling, colorBlending, pipelineLayout, renderPass);

if (vkCreateGraphicsPipelines(logicalDevice, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &graphicsPipeline) != VK_SUCCESS) {
    throw std::runtime_error("failed to create graphics pipeline!");
}

下面是createPipelineInfo函数:

inline static VkGraphicsPipelineCreateInfo createPipelineInfo(
    const std::array<VkPipelineShaderStageCreateInfo, 2> shaderStages,
    const VkPipelineVertexInputStateCreateInfo& vertexInputInfo,
    const VkPipelineInputAssemblyStateCreateInfo& inputAssembly,
    const VkPipelineViewportStateCreateInfo& viewportState,
    const VkPipelineRasterizationStateCreateInfo& rasterizer,
    const VkPipelineMultisampleStateCreateInfo& multisampling,
    const VkPipelineColorBlendStateCreateInfo& colorBlending,
    const VkPipelineLayout& pipelineLayout,
    const VkRenderPass& renderPass) {

    VkGraphicsPipelineCreateInfo pipelineInfo{};

    //Shader Stage
    pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
    pipelineInfo.stageCount = 2;
    pipelineInfo.pStages = shaderStages.data();

    //Fixed Pipeline Stage
    pipelineInfo.pVertexInputState = &vertexInputInfo;
    pipelineInfo.pInputAssemblyState = &inputAssembly;
    pipelineInfo.pViewportState = &viewportState;
    pipelineInfo.pRasterizationState = &rasterizer;
    pipelineInfo.pMultisampleState = &multisampling;
    //pipelineInfo.pDepthStencilState = &depthStencil;
    pipelineInfo.pColorBlendState = &colorBlending;
    pipelineInfo.pDynamicState = nullptr; // Optional

    //Pipeline Layout
    pipelineInfo.layout = pipelineLayout;
    pipelineInfo.renderPass = renderPass;
    pipelineInfo.subpass = 0;
    pipelineInfo.basePipelineHandle = VK_NULL_HANDLE;

    return pipelineInfo;
}

另一方面,如果我复制函数体并将其转储到函数调用的位置,一切都很好。

VkGraphicsPipelineCreateInfo pipelineInfo{};

//Shader Stage
pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
pipelineInfo.stageCount = 2;
pipelineInfo.pStages = shaderStages.data();

//Fixed Pipeline Stage
pipelineInfo.pVertexInputState = &vertexInputInfo;
pipelineInfo.pInputAssemblyState = &inputAssembly;
pipelineInfo.pViewportState = &viewportState;
pipelineInfo.pRasterizationState = &rasterizer;
pipelineInfo.pMultisampleState = &multisampling;
//pipelineInfo.pDepthStencilState = &depthStencil;
pipelineInfo.pColorBlendState = &colorBlending;
pipelineInfo.pDynamicState = nullptr; // Optional

//Pipeline Layout
pipelineInfo.layout = pipelineLayout;
pipelineInfo.renderPass = renderPass;
pipelineInfo.subpass = 0;
pipelineInfo.basePipelineHandle = VK_NULL_HANDLE;


if (vkCreateGraphicsPipelines(logicalDevice, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &graphicsPipeline) != VK_SUCCESS) {
    throw std::runtime_error("failed to create graphics pipeline!");
}

我试图弄清楚为什么编译器正在优化函数调用,但未能尝试开发一个不涉及转储函数体来代替对函数的每次调用的变通方法。

胡乱猜测:这个参数是copy传递的

const std::array<VkPipelineShaderStageCreateInfo, 2> shaderStages

所以当使用 data 方法调用获取其内容的地址时:

pipelineInfo.pStages = shaderStages.data();

您调用了未定义的行为。编译器不够智能,无法 1) 由于调用的复杂性而警告您引用临时对象,以及 2) 它不会自动对参数传递执行复制省略。

修复:通过引用传递它(请注意,所有其他参数使用引用模式是有原因的)

const std::array<VkPipelineShaderStageCreateInfo, 2> &shaderStages