Skip to content

Commit fc7857c

Browse files
authored
[CIR] Add PointerLikeType interface support for cir::PointerType (#139768)
This adds code to attach the OpenACC PointerLikeType interface to cir::PointerType, along with a unit test for the interface.
1 parent 4f663cc commit fc7857c

File tree

11 files changed

+553
-1
lines changed

11 files changed

+553
-1
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file contains external dialect interfaces for CIR.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef CLANG_CIR_DIALECT_OPENACC_CIROPENACCTYPEINTERFACES_H
14+
#define CLANG_CIR_DIALECT_OPENACC_CIROPENACCTYPEINTERFACES_H
15+
16+
#include "mlir/Dialect/OpenACC/OpenACC.h"
17+
18+
namespace cir::acc {
19+
20+
template <typename T>
21+
struct OpenACCPointerLikeModel
22+
: public mlir::acc::PointerLikeType::ExternalModel<
23+
OpenACCPointerLikeModel<T>, T> {
24+
mlir::Type getElementType(mlir::Type pointer) const {
25+
return mlir::cast<T>(pointer).getPointee();
26+
}
27+
mlir::acc::VariableTypeCategory
28+
getPointeeTypeCategory(mlir::Type pointer,
29+
mlir::TypedValue<mlir::acc::PointerLikeType> varPtr,
30+
mlir::Type varType) const;
31+
};
32+
33+
} // namespace cir::acc
34+
35+
#endif // CLANG_CIR_DIALECT_OPENACC_CIROPENACCTYPEINTERFACES_H
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef CLANG_CIR_DIALECT_OPENACC_REGISTEROPENACCEXTENSIONS_H
10+
#define CLANG_CIR_DIALECT_OPENACC_REGISTEROPENACCEXTENSIONS_H
11+
12+
namespace mlir {
13+
class DialectRegistry;
14+
} // namespace mlir
15+
16+
namespace cir::acc {
17+
18+
void registerOpenACCExtensions(mlir::DialectRegistry &registry);
19+
20+
} // namespace cir::acc
21+
22+
#endif // CLANG_CIR_DIALECT_OPENACC_REGISTEROPENACCEXTENSIONS_H

clang/lib/CIR/CodeGen/CIRGenerator.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "clang/AST/DeclGroup.h"
1919
#include "clang/CIR/CIRGenerator.h"
2020
#include "clang/CIR/Dialect/IR/CIRDialect.h"
21+
#include "clang/CIR/Dialect/OpenACC/RegisterOpenACCExtensions.h"
2122

2223
using namespace cir;
2324
using namespace clang;
@@ -38,6 +39,12 @@ void CIRGenerator::Initialize(ASTContext &astContext) {
3839
mlirContext = std::make_unique<mlir::MLIRContext>();
3940
mlirContext->loadDialect<cir::CIRDialect>();
4041
mlirContext->getOrLoadDialect<mlir::acc::OpenACCDialect>();
42+
43+
// Register extensions to integrate CIR types with OpenACC.
44+
mlir::DialectRegistry registry;
45+
cir::acc::registerOpenACCExtensions(registry);
46+
mlirContext->appendDialectRegistry(registry);
47+
4148
cgm = std::make_unique<clang::CIRGen::CIRGenModule>(
4249
*mlirContext.get(), astContext, codeGenOpts, diags);
4350
}

clang/lib/CIR/CodeGen/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ add_clang_library(clangCIR
3535
clangBasic
3636
clangLex
3737
${dialect_libs}
38+
CIROpenACCSupport
3839
MLIRCIR
3940
MLIRCIRInterfaces
4041
)

clang/lib/CIR/Dialect/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
add_subdirectory(IR)
2+
add_subdirectory(OpenACC)
23
add_subdirectory(Transforms)
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// Implementation of external dialect interfaces for CIR.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "clang/CIR/Dialect/OpenACC/CIROpenACCTypeInterfaces.h"
14+
#include "clang/CIR/Dialect/IR/CIRDialect.h"
15+
#include "clang/CIR/Dialect/IR/CIRTypes.h"
16+
17+
namespace cir::acc {
18+
19+
mlir::Type getBaseType(mlir::Value varPtr) {
20+
mlir::Operation *op = varPtr.getDefiningOp();
21+
assert(op && "Expected a defining operation");
22+
23+
// This is the variable definition we're looking for.
24+
if (auto allocaOp = mlir::dyn_cast<cir::AllocaOp>(*op))
25+
return allocaOp.getAllocaType();
26+
27+
// Look through casts to the source pointer.
28+
if (auto castOp = mlir::dyn_cast<cir::CastOp>(*op))
29+
return getBaseType(castOp.getSrc());
30+
31+
// Follow the source of ptr strides.
32+
if (auto ptrStrideOp = mlir::dyn_cast<cir::PtrStrideOp>(*op))
33+
return getBaseType(ptrStrideOp.getBase());
34+
35+
if (auto getMemberOp = mlir::dyn_cast<cir::GetMemberOp>(*op))
36+
return getBaseType(getMemberOp.getAddr());
37+
38+
return mlir::cast<cir::PointerType>(varPtr.getType()).getPointee();
39+
}
40+
41+
template <>
42+
mlir::acc::VariableTypeCategory
43+
OpenACCPointerLikeModel<cir::PointerType>::getPointeeTypeCategory(
44+
mlir::Type pointer, mlir::TypedValue<mlir::acc::PointerLikeType> varPtr,
45+
mlir::Type varType) const {
46+
mlir::Type eleTy = getBaseType(varPtr);
47+
48+
if (auto mappableTy = mlir::dyn_cast<mlir::acc::MappableType>(eleTy))
49+
return mappableTy.getTypeCategory(varPtr);
50+
51+
if (isAnyIntegerOrFloatingPointType(eleTy) ||
52+
mlir::isa<cir::BoolType>(eleTy) || mlir::isa<cir::PointerType>(eleTy))
53+
return mlir::acc::VariableTypeCategory::scalar;
54+
if (mlir::isa<cir::ArrayType>(eleTy))
55+
return mlir::acc::VariableTypeCategory::array;
56+
if (mlir::isa<cir::RecordType>(eleTy))
57+
return mlir::acc::VariableTypeCategory::composite;
58+
if (mlir::isa<cir::FuncType>(eleTy) || mlir::isa<cir::VectorType>(eleTy))
59+
return mlir::acc::VariableTypeCategory::nonscalar;
60+
61+
// Without further checking, this type cannot be categorized.
62+
return mlir::acc::VariableTypeCategory::uncategorized;
63+
}
64+
65+
} // namespace cir::acc
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
add_clang_library(CIROpenACCSupport
2+
CIROpenACCTypeInterfaces.cpp
3+
RegisterOpenACCExtensions.cpp
4+
5+
DEPENDS
6+
MLIRCIRTypeConstraintsIncGen
7+
8+
LINK_LIBS PUBLIC
9+
MLIRIR
10+
MLIRCIR
11+
MLIROpenACCDialect
12+
)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// Registration for OpenACC extensions as applied to CIR dialect.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "clang/CIR/Dialect/OpenACC/RegisterOpenACCExtensions.h"
14+
#include "clang/CIR/Dialect/IR/CIRDialect.h"
15+
#include "clang/CIR/Dialect/IR/CIRTypes.h"
16+
#include "clang/CIR/Dialect/OpenACC/CIROpenACCTypeInterfaces.h"
17+
18+
namespace cir::acc {
19+
20+
void registerOpenACCExtensions(mlir::DialectRegistry &registry) {
21+
registry.addExtension(+[](mlir::MLIRContext *ctx, cir::CIRDialect *dialect) {
22+
cir::PointerType::attachInterface<
23+
OpenACCPointerLikeModel<cir::PointerType>>(*ctx);
24+
});
25+
}
26+
27+
} // namespace cir::acc

clang/unittests/CIR/CMakeLists.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
set(MLIR_INCLUDE_DIR ${LLVM_MAIN_SRC_DIR}/../mlir/include )
2+
set(MLIR_TABLEGEN_OUTPUT_DIR ${CMAKE_BINARY_DIR}/tools/mlir/include)
3+
include_directories(SYSTEM ${MLIR_INCLUDE_DIR})
4+
include_directories(${MLIR_TABLEGEN_OUTPUT_DIR})
5+
6+
add_distinct_clang_unittest(CIRUnitTests
7+
PointerLikeTest.cpp
8+
LLVM_COMPONENTS
9+
Core
10+
11+
LINK_LIBS
12+
MLIRCIR
13+
CIROpenACCSupport
14+
MLIRIR
15+
MLIROpenACCDialect
16+
)

0 commit comments

Comments
 (0)