c++ - 限制类成员指针的限定符
问题描述
我有一个这样的程序,我必须使用一个类来抽象指针。我希望编译器在main
没有指针别名的情况下优化下面函数中的展开循环。在这种情况下如何有效地使用__restrict
构造?我正在使用带有 RISC-V 后端的 Clang。
extern int* getPtr(int n); // Runtime function providing data
class RemotePtr {
private:
int* a;
public:
RemotePtr(int* a) : a(a) {}
int& at(int n) {
return a[n];
}
};
int main(int argc, char** argv) {
int* A = getPtr(0);
int* B = getPtr(1);
RemotePtr a(A);
RemotePtr b(B);
// This loop should be compiled assuming no aliasing between
// pointers `a.a` and `b.a`
#pragma unroll 4
for(int i=0; i<4; ++i) {
a.at(i) += b.at(i);
}
return 0;
}
以下是经典用例,__restrict
当我编译它时,我noalias
会根据需要在 LLVM IR 中看到注释。
int* A;
int* B;
void vec_add(int* __restrict a, int* __restrict b, int n) {
#pragma unroll 4
for(int i=0; i<n; ++i) {
a[i] += b[i];
}
}
int main(int argc, char** argv) {
vec_add(A, B, 4);
return 0;
}
结果IR的片段:
; Function Attrs: nofree norecurse nounwind
define dso_local void @_Z7vec_addPiS_i(i32* noalias nocapture %a, i32* noalias nocapture readonly %b, i32 %n) local_unnamed_a
ddr #0 {
entry:
%cmp6 = icmp sgt i32 %n, 0
br i1 %cmp6, label %for.body.preheader, label %for.cond.cleanup
for.body.preheader: ; preds = %entry
%0 = add nsw i32 %n, -1
%xtraiter = and i32 %n, 3
最后,将行更改为a.at(i) += b.at(i);
带限制的 lambda 函数调用似乎有效:
([&](int& __restrict a, int& __restrict b) {a += b;})(a.at(i), b.at(i));
我想,这给了我想要的:
; Function Attrs: norecurse nounwind
define dso_local i32 @main(i32 %argc, i8** nocapture readnone %argv) local_unnamed_addr #0 {
entry:
%0 = load i32*, i32** @A, align 4, !tbaa !3
%1 = load i32*, i32** @B, align 4, !tbaa !3
%2 = load i32, i32* %1, align 4, !tbaa !7, !alias.scope !9, !noalias !12
%3 = load i32, i32* %0, align 4, !tbaa !7, !alias.scope !12, !noalias !9
但是我想在类中抽象它,而不需要用户编写一个 lambda 构造来实现易于使用的结果,以及避免错过这个优化。
解决方案
推荐阅读
- amazon-web-services - 从 Lambda 捕获 AWS Cloud Watch 自定义错误代码
- r - 如何减少R中数据的大小?
- html - 汉堡菜单未显示在移动 html 和 CSS3Menu 上
- python - 麻烦从 csv 文件导入数据,循环通过代码,从 finviz.com 抓取数据,并导出到 csv 文件以进行更多分析
- python - 为什么 SciPy 优化曲线拟合需要 x 数据从 0 开始
- google-apps-script - 通过电子邮件发送的 Google 表格图表中的限制数量
- r - 从 Kaggle 读取数据集
- java - 动态下拉从jsp返回一个null
- python - 如何在添加之前检查 Google 日历以查看活动是否已存在
- meteor - 使用 jquery 错误测试环境负载