Skip to content

Perfect forwarding of arguments to CreateOpNode would be nice #1201

@haved

Description

@haved

The implementation is currently like

template<typename OperatorType, typename... OperatorArguments>
SimpleNode &
CreateOpNode(const std::vector<Output *> & operands, OperatorArguments... operatorArguments)
{
  JLM_ASSERT(!operands.empty());
  return SimpleNode::Create(
      *operands[0]->region(),
      std::make_unique<OperatorType>(std::move(operatorArguments)...),
      operands);
}

but we can avoid potential copying of arguments by using perfect forwarding, like so:

template<typename OperatorType, typename... OperatorArguments>
SimpleNode &
CreateOpNode(const std::vector<Output *> & operands, OperatorArguments&&... operatorArguments)
{
  JLM_ASSERT(!operands.empty());
  return SimpleNode::Create(
      *operands[0]->region(),
      std::make_unique<OperatorType>(std::forward<OperatorArguments>(operatorArguments)...),
      operands);
}

The problem is that this causes some annoying false-positive warnings.
The following snippet from IfConversionTests.cpp, for example:

const auto & c0 = jlm::rvsdg::CreateOpNode<jlm::rvsdg::ctlconstant_op>(
      *gammaNode0->subregion(0),
      jlm::rvsdg::ControlValueRepresentation(0, 2));

gives

tests/jlm/llvm/opt/IfConversionTests.cpp: In function ‘void EmptyGammaWithTwoSubregions()’:
tests/jlm/llvm/opt/IfConversionTests.cpp:180:16: error: possibly dangling reference to a temporary [-Werror=dangling-reference]
  180 |   const auto & c0 = jlm::rvsdg::CreateOpNode<jlm::rvsdg::ctlconstant_op>(
      |                ^~
compilation terminated due to -Wfatal-errors.
cc1plus: all warnings being treated as errors

It is possible to wrap the definitions of CreateOpNode with _Pragma("GCC diagnostic ignored \"-Wdangling-reference\""), but I'm not a huge fan of it. example

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions