如何在 clang++ 中禁用自动"pass by pointer"优化?
How to disable automatic "pass by pointer" optimization in clang++?
我有一个函数
void X(Object o)
{
....
}
当我编译它时,我看到clang将其签名更改为
void X(Object* o)
这很不方便,因为我直接从一些llvm IR代码中使用这个函数。如何禁止它做这种优化?
编辑:最小工作示例:
#include <stdio.h>
class Object
{
public:
Object();
~Object();
int* pointer;
};
void Function(Object o)
{
o.pointer = 0;
}
int main()
{
Object a;
Function(a);
return 0;
}
clang++ tst.cpp -emit-llvm -O0 tst.cpp -S -std=c++11
Function
被翻译成:
define void @_Z8Function6Object(%class.Object* %o) nounwind uwtable {
%1 = getelementptr inbounds %class.Object* %o, i32 0, i32 0
store i32* null, i32** %1, align 8
ret void
}
您需要添加选项-mdisable-fp-elim
禁用帧指针消除优化。
在这里我找到了这个选项:clang option
这里我很好地解释了为什么clang这样做:理解选项'省略帧指针'
*编辑:*
经过检查,我发现如下:
- 你的对象在编译后正确地通过拷贝传递:
的例子:
#include <stdio.h>
#include <iostream>
class Object
{
public:
std::string test;
Object() {
this->test = "I'm an object";
std::cout << "Object created" << std::endl;
}
Object(Object &o) {
this->test = "I'm a object copy";
std::cout << "Object copy created" << std::endl;
}
~Object() {
}
int* pointer;
};
void Function(Object o)
{
o.pointer = 0;
std::cout << o.test << std::endl;
}
int main()
{
Object a;
Function(a);
std::cout << a.test << std::endl;
return 0;
}
输出:
对象创建
创建对象副本
我是object copy
I'm an object
- 第二点:
你可以看到在函数原型
之后; Function Attrs: uwtable
define void @_Z8Function6Object(%class.Object* %o) #3 {
%1 = getelementptr inbounds %class.Object* %o, i32 0, i32 1 // Get
store i32* null, i32** %1, align 8
函数得到对象的副本。你可以在main
中看到对象
所以你的代码似乎工作得很好,事实上;)
相关文章:
- 使用模板进行堆栈实现; "name followed by :: must be a class or namespace"
- 将 std::thread by 值推送到列表中
- 如何检查函数是否在LLVM Instrumentation pass的ModulePass的系统头文件中定义?
- MATLAB to C++: csvread() not supported by MATLAB Coder
- 如何在C++中设置演员的"Render Custom Depth Pass"和"Depth Stencil Value"?
- Makefile by ocaml 和 cpp 扩展名
- C 语言和 C++ 中的"pass by reference"究竟有什么区别?
- "Want Speed? Pass by value"有多真实
- std::thread pass-by-reference调用复制构造函数
- 为什么程序员说"pass by reference"真的很"passing references by value?"为什么这很重要?
- 将特征类型与 boost::bind 一起使用是否会自动违反 Eigen 的"only pass by reference"规则?
- 采用"pass by reference"值的递归函数的包装函数
- "return-by-reference"或"pass-by-reference"参数何时与constexpr兼容?
- 如何在 clang++ 中禁用自动"pass by pointer"优化?
- operator=使用move assign和pass by value copy assign的歧义重载
- 在pass-by-const-reference方法内部的c++ pass-by-non-const-reference
- 如何在 C 语言中模仿C++ "pass array(pointer) by reference"?
- 两个方法声明都"pass by reference"吗?
- 如何在C++中编写'pass reference by value'包装器?
- Pass-by-value-Result?