编译器“优化”出对象初始化函数

问题描述

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

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

xls = pd.ExcelFile('path')
d = pd.read_excel(xls,'Sheet_1')

for i,j in enumerate(d.DATE):
    if j == 'A':
        d.DATE[i].replace("A"," ")

以下是 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!");
}

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

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;
}

我试图弄清楚为什么编译器会优化函数调用,而在尝试开发一种不涉及转储函数体代替每次函数调用解决方法时失败了。

>

解决方法

胡乱猜测:这个参数是通过copy方式传递的

const std::array<VkPipelineShaderStageCreateInfo,2> shaderStages

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

pipelineInfo.pStages = shaderStages.data();

您调用了未定义的行为。编译器不够聪明,无法 1) 由于调用的复杂性而警告您不要引用临时文件,并且 2) 它不会自动执行参数传递时的复制省略。

修复:通过引用传递(注意所有其他参数出于某种原因使用引用模式)

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