Skip to content

[OpenMP] Don't emit redundant zero-sized mapping nodes for overlapped structs #148947

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
157 changes: 110 additions & 47 deletions clang/lib/CodeGen/CGOpenMPRuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7080,6 +7080,110 @@ class MappableExprsHandler {
return ConstLength.getSExtValue() != 1;
}

/// A helper class to copy structures with overlapped elements, i.e. those
/// which have mappings of both "s" and "s.mem". Consecutive elements that
/// are not explicitly copied have mapping nodes synthesized for them,
/// taking care to avoid generating zero-sized copies.
class CopyOverlappedEntryGaps {
CodeGenFunction &CGF;
MapCombinedInfoTy &CombinedInfo;
OpenMPOffloadMappingFlags Flags = OpenMPOffloadMappingFlags::OMP_MAP_NONE;
const ValueDecl *MapDecl = nullptr;
const Expr *MapExpr = nullptr;
Address BP = Address::invalid();
bool IsNonContiguous = false;
uint64_t DimSize = 0;
// These elements track the position as the struct is iterated over
// (in order of increasing element address).
const RecordDecl *LastParent = nullptr;
uint64_t Cursor = 0;
unsigned LastIndex = -1u;
Address LB = Address::invalid();

public:
CopyOverlappedEntryGaps(CodeGenFunction &CGF,
MapCombinedInfoTy &CombinedInfo,
OpenMPOffloadMappingFlags Flags,
const ValueDecl *MapDecl, const Expr *MapExpr,
Address BP, Address LB, bool IsNonContiguous,
uint64_t DimSize)
: CGF(CGF), CombinedInfo(CombinedInfo), Flags(Flags), MapDecl(MapDecl),
MapExpr(MapExpr), BP(BP), LB(LB), IsNonContiguous(IsNonContiguous),
DimSize(DimSize) {}

void processField(
const OMPClauseMappableExprCommon::MappableComponent &MC,
const FieldDecl *FD,
llvm::function_ref<LValue(CodeGenFunction &, const MemberExpr *)>
EmitMemberExprBase) {
const RecordDecl *RD = FD->getParent();
const ASTRecordLayout &RL = CGF.getContext().getASTRecordLayout(RD);
uint64_t FieldOffset = RL.getFieldOffset(FD->getFieldIndex());
uint64_t FieldSize =
CGF.getContext().getTypeSize(FD->getType().getCanonicalType());
Address ComponentLB = Address::invalid();

if (FD->getType()->isLValueReferenceType()) {
const auto *ME = cast<MemberExpr>(MC.getAssociatedExpression());
LValue BaseLVal = EmitMemberExprBase(CGF, ME);
ComponentLB =
CGF.EmitLValueForFieldInitialization(BaseLVal, FD).getAddress();
} else {
ComponentLB =
CGF.EmitOMPSharedLValue(MC.getAssociatedExpression()).getAddress();
}

if (!LastParent)
LastParent = RD;
if (FD->getParent() == LastParent) {
if (FD->getFieldIndex() != LastIndex + 1)
copyUntilField(FD, ComponentLB);
} else {
LastParent = FD->getParent();
if (((int64_t)FieldOffset - (int64_t)Cursor) > 0)
copyUntilField(FD, ComponentLB);
}
Cursor = FieldOffset + FieldSize;
LastIndex = FD->getFieldIndex();
LB = CGF.Builder.CreateConstGEP(ComponentLB, 1);
}

void copyUntilField(const FieldDecl *FD, Address ComponentLB) {
llvm::Value *ComponentLBPtr = ComponentLB.emitRawPointer(CGF);
llvm::Value *LBPtr = LB.emitRawPointer(CGF);
llvm::Value *Size =
CGF.Builder.CreatePtrDiff(CGF.Int8Ty, ComponentLBPtr, LBPtr);
copySizedChunk(LBPtr, Size);
}

void copyUntilEnd(Address HB) {
if (LastParent) {
const ASTRecordLayout &RL =
CGF.getContext().getASTRecordLayout(LastParent);
if ((uint64_t)CGF.getContext().toBits(RL.getSize()) <= Cursor)
return;
}
llvm::Value *LBPtr = LB.emitRawPointer(CGF);
llvm::Value *Size = CGF.Builder.CreatePtrDiff(
CGF.Int8Ty, CGF.Builder.CreateConstGEP(HB, 1).emitRawPointer(CGF),
LBPtr);
copySizedChunk(LBPtr, Size);
}

void copySizedChunk(llvm::Value *Base, llvm::Value *Size) {
CombinedInfo.Exprs.emplace_back(MapDecl, MapExpr);
CombinedInfo.BasePointers.push_back(BP.emitRawPointer(CGF));
CombinedInfo.DevicePtrDecls.push_back(nullptr);
CombinedInfo.DevicePointers.push_back(DeviceInfoTy::None);
CombinedInfo.Pointers.push_back(Base);
CombinedInfo.Sizes.push_back(
CGF.Builder.CreateIntCast(Size, CGF.Int64Ty, /*isSigned=*/true));
CombinedInfo.Types.push_back(Flags);
CombinedInfo.Mappers.push_back(nullptr);
CombinedInfo.NonContigInfo.Dims.push_back(IsNonContiguous ? DimSize : 1);
}
};

/// Generate the base pointers, section pointers, sizes, map type bits, and
/// user-defined mappers (all included in \a CombinedInfo) for the provided
/// map type, map or motion modifiers, and expression components.
Expand Down Expand Up @@ -7570,63 +7674,22 @@ class MappableExprsHandler {
getMapTypeBits(MapType, MapModifiers, MotionModifiers, IsImplicit,
/*AddPtrFlag=*/false,
/*AddIsTargetParamFlag=*/false, IsNonContiguous);
llvm::Value *Size = nullptr;
CopyOverlappedEntryGaps CopyGaps(CGF, CombinedInfo, Flags, MapDecl,
MapExpr, BP, LB, IsNonContiguous,
DimSize);
// Do bitcopy of all non-overlapped structure elements.
for (OMPClauseMappableExprCommon::MappableExprComponentListRef
Component : OverlappedElements) {
Address ComponentLB = Address::invalid();
for (const OMPClauseMappableExprCommon::MappableComponent &MC :
Component) {
if (const ValueDecl *VD = MC.getAssociatedDeclaration()) {
const auto *FD = dyn_cast<FieldDecl>(VD);
if (FD && FD->getType()->isLValueReferenceType()) {
const auto *ME =
cast<MemberExpr>(MC.getAssociatedExpression());
LValue BaseLVal = EmitMemberExprBase(CGF, ME);
ComponentLB =
CGF.EmitLValueForFieldInitialization(BaseLVal, FD)
.getAddress();
} else {
ComponentLB =
CGF.EmitOMPSharedLValue(MC.getAssociatedExpression())
.getAddress();
if (const auto *FD = dyn_cast<FieldDecl>(VD)) {
CopyGaps.processField(MC, FD, EmitMemberExprBase);
}
llvm::Value *ComponentLBPtr = ComponentLB.emitRawPointer(CGF);
llvm::Value *LBPtr = LB.emitRawPointer(CGF);
Size = CGF.Builder.CreatePtrDiff(CGF.Int8Ty, ComponentLBPtr,
LBPtr);
break;
}
}
assert(Size && "Failed to determine structure size");
CombinedInfo.Exprs.emplace_back(MapDecl, MapExpr);
CombinedInfo.BasePointers.push_back(BP.emitRawPointer(CGF));
CombinedInfo.DevicePtrDecls.push_back(nullptr);
CombinedInfo.DevicePointers.push_back(DeviceInfoTy::None);
CombinedInfo.Pointers.push_back(LB.emitRawPointer(CGF));
CombinedInfo.Sizes.push_back(CGF.Builder.CreateIntCast(
Size, CGF.Int64Ty, /*isSigned=*/true));
CombinedInfo.Types.push_back(Flags);
CombinedInfo.Mappers.push_back(nullptr);
CombinedInfo.NonContigInfo.Dims.push_back(IsNonContiguous ? DimSize
: 1);
LB = CGF.Builder.CreateConstGEP(ComponentLB, 1);
}
CombinedInfo.Exprs.emplace_back(MapDecl, MapExpr);
CombinedInfo.BasePointers.push_back(BP.emitRawPointer(CGF));
CombinedInfo.DevicePtrDecls.push_back(nullptr);
CombinedInfo.DevicePointers.push_back(DeviceInfoTy::None);
CombinedInfo.Pointers.push_back(LB.emitRawPointer(CGF));
llvm::Value *LBPtr = LB.emitRawPointer(CGF);
Size = CGF.Builder.CreatePtrDiff(
CGF.Int8Ty, CGF.Builder.CreateConstGEP(HB, 1).emitRawPointer(CGF),
LBPtr);
CombinedInfo.Sizes.push_back(
CGF.Builder.CreateIntCast(Size, CGF.Int64Ty, /*isSigned=*/true));
CombinedInfo.Types.push_back(Flags);
CombinedInfo.Mappers.push_back(nullptr);
CombinedInfo.NonContigInfo.Dims.push_back(IsNonContiguous ? DimSize
: 1);
CopyGaps.copyUntilEnd(HB);
break;
}
llvm::Value *Size = getExprTypeSize(I->getAssociatedExpression());
Expand Down
52 changes: 52 additions & 0 deletions clang/test/OpenMP/copy-gaps-1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// RUN: %clang_cc1 -verify -triple x86_64-pc-linux-gnu -fopenmp-targets=amdgcn-amd-amdhsa -fopenmp -emit-llvm %s -o - | FileCheck %s
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

auto generate check lines pls

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one though I'm not sure how to do -- the check lines for the new copy-gaps tests are manually written, as it appears were the check lines for the altered test target_map_codegen_35.cpp (which seems to be the only extant test that this patch affects). Would it be better to have auto-generated check lines for the whole generated output for the new tests? That seems like it'd be more brittle, to me.

// expected-no-diagnostics

struct S {
int x;
int y;
int z;
int *p1;
int *p2;
};

struct T : public S {
int a;
int b;
int c;
};

int main() {
T v;

#pragma omp target map(tofrom: v, v.x, v.y, v.z, v.p1[:8], v.a, v.b, v.c)
{
v.x++;
v.y += 2;
v.z += 3;
v.p1[0] += 4;
v.a += 7;
v.b += 5;
v.c += 6;
}

return 0;
}

// CHECK: [[CSTSZ:@.+]] = private {{.*}}constant [10 x i64] [i64 0, i64 0, i64 0, i64 4, i64 4, i64 4, i64 32, i64 4, i64 4, i64 4]
// CHECK: [[CSTTY:@.+]] = private {{.*}}constant [10 x i64] [i64 [[#0x20]], i64 [[#0x1000000000003]], i64 [[#0x1000000000003]], i64 [[#0x1000000000003]], i64 [[#0x1000000000003]], i64 [[#0x1000000000003]], i64 [[#0x1000000000013]], i64 [[#0x1000000000003]], i64 [[#0x1000000000003]], i64 [[#0x1000000000003]]]

// CHECK-DAG: call i32 @__tgt_target_kernel(ptr @{{.+}}, i64 -1, i32 -1, i32 0, ptr @.{{.+}}.region_id, ptr [[ARGS:%.+]])
// CHECK-DAG: [[KSIZE:%.+]] = getelementptr inbounds {{.+}}[[ARGS]], i32 0, i32 4
// CHECK-DAG: store ptr [[SZBASE:%.+]], ptr [[KSIZE]], align 8
// CHECK-DAG: [[SZBASE]] = getelementptr inbounds [10 x i64], ptr [[SIZES:%[^,]*]], i32 0, i32 0

// Check for filling of four non-constant size elements here: the whole struct
// size, the (padded) region covering p1 & p2, and the padding at the end of
// struct T.

// CHECK-DAG: [[STR:%.+]] = getelementptr inbounds [10 x i64], ptr [[SIZES]], i32 0, i32 0
// CHECK-DAG: store i64 %{{.+}}, ptr [[STR]], align 8
// CHECK-DAG: [[P1P2:%.+]] = getelementptr inbounds [10 x i64], ptr [[SIZES]], i32 0, i32 1
// CHECK-DAG: store i64 %{{.+}}, ptr [[P1P2]], align 8
// CHECK-DAG: [[PAD:%.+]] = getelementptr inbounds [10 x i64], ptr [[SIZES]], i32 0, i32 2
// CHECK-DAG: store i64 %{{.+}}, ptr [[PAD]], align 8
52 changes: 52 additions & 0 deletions clang/test/OpenMP/copy-gaps-2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// RUN: %clang_cc1 -verify -triple x86_64-pc-linux-gnu -fopenmp-targets=amdgcn-amd-amdhsa -fopenmp -emit-llvm %s -o - | FileCheck %s
// expected-no-diagnostics

struct S {
int x;
int y;
int z;
};

struct M : public S {
int mid;
};

struct T : public M {
int a;
int b;
int c;
};

int main() {
T v;

#pragma omp target map(tofrom: v, v.y, v.z, v.a)
{
v.y++;
v.z += 2;
v.a += 3;
v.mid += 5;
}

return 0;
}

// CHECK: [[CSTSZ:@.+]] = private {{.*}}constant [7 x i64] [i64 0, i64 0, i64 0, i64 0, i64 4, i64 4, i64 4]
// CHECK: [[CSTTY:@.+]] = private {{.*}}constant [7 x i64] [i64 [[#0x20]], i64 [[#0x1000000000003]], i64 [[#0x1000000000003]], i64 [[#0x1000000000003]], i64 [[#0x1000000000003]], i64 [[#0x1000000000003]], i64 [[#0x1000000000003]]]

// CHECK-DAG: call i32 @__tgt_target_kernel(ptr @{{.+}}, i64 -1, i32 -1, i32 0, ptr @.{{.+}}.region_id, ptr [[ARGS:%.+]])
// CHECK-DAG: [[KSIZE:%.+]] = getelementptr inbounds {{.+}}[[ARGS]], i32 0, i32 4
// CHECK-DAG: store ptr [[SZBASE:%.+]], ptr [[KSIZE]], align 8
// CHECK-DAG: [[SZBASE]] = getelementptr inbounds [7 x i64], ptr [[SIZES:%[^,]*]], i32 0, i32 0

// Fill four non-constant size elements here: the whole struct size, the region
// covering v.x, the region covering v.mid and the region covering v.b and v.c.

// CHECK-DAG: [[STR:%.+]] = getelementptr inbounds [7 x i64], ptr [[SIZES]], i32 0, i32 0
// CHECK-DAG: store i64 %{{.+}}, ptr [[STR]], align 8
// CHECK-DAG: [[X:%.+]] = getelementptr inbounds [7 x i64], ptr [[SIZES]], i32 0, i32 1
// CHECK-DAG: store i64 %{{.+}}, ptr [[X]], align 8
// CHECK-DAG: [[MID:%.+]] = getelementptr inbounds [7 x i64], ptr [[SIZES]], i32 0, i32 2
// CHECK-DAG: store i64 %{{.+}}, ptr [[MID]], align 8
// CHECK-DAG: [[BC:%.+]] = getelementptr inbounds [7 x i64], ptr [[SIZES]], i32 0, i32 3
// CHECK-DAG: store i64 %{{.+}}, ptr [[BC]], align 8
46 changes: 46 additions & 0 deletions clang/test/OpenMP/copy-gaps-3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// RUN: %clang_cc1 -verify -triple x86_64-pc-linux-gnu -fopenmp-targets=amdgcn-amd-amdhsa -fopenmp -emit-llvm %s -o - | FileCheck %s
// expected-no-diagnostics

struct S {
int x;
int y;
int z;
};

struct T : public S {
int a;
int b;
int c;
};

int main() {
T v;

// This one should have no gap between v.z & v.a.
#pragma omp target map(tofrom: v, v.y, v.z, v.a)
{
v.y++;
v.z += 2;
v.a += 3;
}

return 0;
}

// CHECK: [[CSTSZ:@.+]] = private {{.*}}constant [6 x i64] [i64 0, i64 0, i64 0, i64 4, i64 4, i64 4]
// CHECK: [[CSTTY:@.+]] = private {{.*}}constant [6 x i64] [i64 [[#0x20]], i64 [[#0x1000000000003]], i64 [[#0x1000000000003]], i64 [[#0x1000000000003]], i64 [[#0x1000000000003]], i64 [[#0x1000000000003]]]

// CHECK-DAG: call i32 @__tgt_target_kernel(ptr @{{.+}}, i64 -1, i32 -1, i32 0, ptr @.{{.+}}.region_id, ptr [[ARGS:%.+]])
// CHECK-DAG: [[KSIZE:%.+]] = getelementptr inbounds {{.+}}[[ARGS]], i32 0, i32 4
// CHECK-DAG: store ptr [[SZBASE:%.+]], ptr [[KSIZE]], align 8
// CHECK-DAG: [[SZBASE]] = getelementptr inbounds [6 x i64], ptr [[SIZES:%[^,]*]], i32 0, i32 0

// Fill three non-constant size elements here: the whole struct size, the region
// covering v.x, and the region covering v.b and v.c.

// CHECK-DAG: [[STR:%.+]] = getelementptr inbounds [6 x i64], ptr [[SIZES]], i32 0, i32 0
// CHECK-DAG: store i64 %{{.+}}, ptr [[STR]], align 8
// CHECK-DAG: [[X:%.+]] = getelementptr inbounds [6 x i64], ptr [[SIZES]], i32 0, i32 1
// CHECK-DAG: store i64 %{{.+}}, ptr [[X]], align 8
// CHECK-DAG: [[BC:%.+]] = getelementptr inbounds [6 x i64], ptr [[SIZES]], i32 0, i32 2
// CHECK-DAG: store i64 %{{.+}}, ptr [[BC]], align 8
48 changes: 48 additions & 0 deletions clang/test/OpenMP/copy-gaps-4.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// RUN: %clang_cc1 -verify -triple x86_64-pc-linux-gnu -fopenmp-targets=amdgcn-amd-amdhsa -fopenmp -emit-llvm %s -o - | FileCheck %s
// expected-no-diagnostics

struct S {
int x;
int y;
char z; // Hidden padding after here...
};

struct T : public S {
int a;
int b;
int c;
};

int main() {
T v;

#pragma omp target map(tofrom: v, v.y, v.z, v.a)
{
v.y++;
v.z += 2;
v.a += 3;
}

return 0;
}

// CHECK: [[CSTSZ:@.+]] = private {{.*}}constant [7 x i64] [i64 0, i64 0, i64 0, i64 0, i64 4, i64 1, i64 4]
// CHECK: [[CSTTY:@.+]] = private {{.*}}constant [7 x i64] [i64 [[#0x20]], i64 [[#0x1000000000003]], i64 [[#0x1000000000003]], i64 [[#0x1000000000003]], i64 [[#0x1000000000003]], i64 [[#0x1000000000003]], i64 [[#0x1000000000003]]]

// CHECK-DAG: call i32 @__tgt_target_kernel(ptr @{{.+}}, i64 -1, i32 -1, i32 0, ptr @.{{.+}}.region_id, ptr [[ARGS:%.+]])
// CHECK-DAG: [[KSIZE:%.+]] = getelementptr inbounds {{.+}}[[ARGS]], i32 0, i32 4
// CHECK-DAG: store ptr [[SZBASE:%.+]], ptr [[KSIZE]], align 8
// CHECK-DAG: [[SZBASE]] = getelementptr inbounds [7 x i64], ptr [[SIZES:%[^,]*]], i32 0, i32 0

// Fill four non-constant size elements here: the whole struct size, the region
// covering v.x, the region covering padding after v.z and the region covering
// v.b and v.c.

// CHECK-DAG: [[STR:%.+]] = getelementptr inbounds [7 x i64], ptr [[SIZES]], i32 0, i32 0
// CHECK-DAG: store i64 %{{.+}}, ptr [[STR]], align 8
// CHECK-DAG: [[X:%.+]] = getelementptr inbounds [7 x i64], ptr [[SIZES]], i32 0, i32 1
// CHECK-DAG: store i64 %{{.+}}, ptr [[X]], align 8
// CHECK-DAG: [[PAD:%.+]] = getelementptr inbounds [7 x i64], ptr [[SIZES]], i32 0, i32 2
// CHECK-DAG: store i64 %{{.+}}, ptr [[PAD]], align 8
// CHECK-DAG: [[BC:%.+]] = getelementptr inbounds [7 x i64], ptr [[SIZES]], i32 0, i32 3
// CHECK-DAG: store i64 %{{.+}}, ptr [[BC]], align 8
Loading