-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[CIR] Add PointerLikeType interface support for cir::PointerType #139768
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This file contains external dialect interfaces for CIR. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef CLANG_CIR_DIALECT_OPENACC_CIROPENACCTYPEINTERFACES_H | ||
#define CLANG_CIR_DIALECT_OPENACC_CIROPENACCTYPEINTERFACES_H | ||
|
||
#include "mlir/Dialect/OpenACC/OpenACC.h" | ||
|
||
namespace cir::acc { | ||
|
||
template <typename T> | ||
struct OpenACCPointerLikeModel | ||
: public mlir::acc::PointerLikeType::ExternalModel< | ||
OpenACCPointerLikeModel<T>, T> { | ||
mlir::Type getElementType(mlir::Type pointer) const { | ||
return mlir::cast<T>(pointer).getPointee(); | ||
} | ||
mlir::acc::VariableTypeCategory | ||
getPointeeTypeCategory(mlir::Type pointer, | ||
mlir::TypedValue<mlir::acc::PointerLikeType> varPtr, | ||
mlir::Type varType) const; | ||
}; | ||
|
||
} // namespace cir::acc | ||
|
||
#endif // CLANG_CIR_DIALECT_OPENACC_CIROPENACCTYPEINTERFACES_H |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef CLANG_CIR_DIALECT_OPENACC_REGISTEROPENACCEXTENSIONS_H | ||
#define CLANG_CIR_DIALECT_OPENACC_REGISTEROPENACCEXTENSIONS_H | ||
|
||
namespace mlir { | ||
class DialectRegistry; | ||
} // namespace mlir | ||
|
||
namespace cir::acc { | ||
|
||
void registerOpenACCExtensions(mlir::DialectRegistry ®istry); | ||
|
||
} // namespace cir::acc | ||
|
||
#endif // CLANG_CIR_DIALECT_OPENACC_REGISTEROPENACCEXTENSIONS_H |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,6 +35,7 @@ add_clang_library(clangCIR | |
clangBasic | ||
clangLex | ||
${dialect_libs} | ||
CIROpenACCSupport | ||
MLIRCIR | ||
MLIRCIRInterfaces | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
add_subdirectory(IR) | ||
add_subdirectory(OpenACC) | ||
add_subdirectory(Transforms) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Implementation of external dialect interfaces for CIR. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "clang/CIR/Dialect/OpenACC/CIROpenACCTypeInterfaces.h" | ||
#include "clang/CIR/Dialect/IR/CIRDialect.h" | ||
#include "clang/CIR/Dialect/IR/CIRTypes.h" | ||
|
||
namespace cir::acc { | ||
|
||
mlir::Type getBaseType(mlir::Value varPtr) { | ||
mlir::Operation *op = varPtr.getDefiningOp(); | ||
assert(op && "Expected a defining operation"); | ||
|
||
// This is the variable definition we're looking for. | ||
if (auto allocaOp = mlir::dyn_cast<cir::AllocaOp>(*op)) | ||
return allocaOp.getAllocaType(); | ||
|
||
// Look through casts to the source pointer. | ||
if (auto castOp = mlir::dyn_cast<cir::CastOp>(*op)) | ||
return getBaseType(castOp.getSrc()); | ||
|
||
// Follow the source of ptr strides. | ||
if (auto ptrStrideOp = mlir::dyn_cast<cir::PtrStrideOp>(*op)) | ||
return getBaseType(ptrStrideOp.getBase()); | ||
|
||
if (auto getMemberOp = mlir::dyn_cast<cir::GetMemberOp>(*op)) | ||
return getBaseType(getMemberOp.getAddr()); | ||
|
||
return mlir::cast<cir::PointerType>(varPtr.getType()).getPointee(); | ||
} | ||
|
||
template <> | ||
mlir::acc::VariableTypeCategory | ||
OpenACCPointerLikeModel<cir::PointerType>::getPointeeTypeCategory( | ||
mlir::Type pointer, mlir::TypedValue<mlir::acc::PointerLikeType> varPtr, | ||
mlir::Type varType) const { | ||
mlir::Type eleTy = getBaseType(varPtr); | ||
|
||
if (auto mappableTy = mlir::dyn_cast<mlir::acc::MappableType>(eleTy)) | ||
return mappableTy.getTypeCategory(varPtr); | ||
|
||
if (isAnyIntegerOrFloatingPointType(eleTy) || | ||
mlir::isa<cir::BoolType>(eleTy) || mlir::isa<cir::PointerType>(eleTy)) | ||
return mlir::acc::VariableTypeCategory::scalar; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems the CIR type system allows computation of interior pointers: https://godbolt.org/z/aPn1j5a69 Here is how we dealt with this case in FIR which allows the same: https://github.com/llvm/llvm-project/blob/main/flang/lib/Optimizer/OpenACC/FIROpenACCTypeInterfaces.cpp#L386 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK, so it's not just a property of the type, but an interpretation of the type within the context of the value it is associated with. That makes sense now for the extra parameters that I'm ignoring here. |
||
if (mlir::isa<cir::ArrayType>(eleTy)) | ||
return mlir::acc::VariableTypeCategory::array; | ||
if (mlir::isa<cir::RecordType>(eleTy)) | ||
return mlir::acc::VariableTypeCategory::composite; | ||
if (mlir::isa<cir::FuncType>(eleTy) || mlir::isa<cir::VectorType>(eleTy)) | ||
return mlir::acc::VariableTypeCategory::nonscalar; | ||
|
||
// Without further checking, this type cannot be categorized. | ||
return mlir::acc::VariableTypeCategory::uncategorized; | ||
} | ||
|
||
} // namespace cir::acc |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
add_clang_library(CIROpenACCSupport | ||
CIROpenACCTypeInterfaces.cpp | ||
RegisterOpenACCExtensions.cpp | ||
|
||
DEPENDS | ||
MLIRCIRTypeConstraintsIncGen | ||
|
||
LINK_LIBS PUBLIC | ||
MLIRIR | ||
MLIRCIR | ||
MLIROpenACCDialect | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Registration for OpenACC extensions as applied to CIR dialect. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "clang/CIR/Dialect/OpenACC/RegisterOpenACCExtensions.h" | ||
#include "clang/CIR/Dialect/IR/CIRDialect.h" | ||
#include "clang/CIR/Dialect/IR/CIRTypes.h" | ||
#include "clang/CIR/Dialect/OpenACC/CIROpenACCTypeInterfaces.h" | ||
|
||
namespace cir::acc { | ||
|
||
void registerOpenACCExtensions(mlir::DialectRegistry ®istry) { | ||
registry.addExtension(+[](mlir::MLIRContext *ctx, cir::CIRDialect *dialect) { | ||
cir::PointerType::attachInterface< | ||
OpenACCPointerLikeModel<cir::PointerType>>(*ctx); | ||
}); | ||
} | ||
|
||
} // namespace cir::acc |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
set(MLIR_INCLUDE_DIR ${LLVM_MAIN_SRC_DIR}/../mlir/include ) | ||
set(MLIR_TABLEGEN_OUTPUT_DIR ${CMAKE_BINARY_DIR}/tools/mlir/include) | ||
include_directories(SYSTEM ${MLIR_INCLUDE_DIR}) | ||
include_directories(${MLIR_TABLEGEN_OUTPUT_DIR}) | ||
|
||
add_distinct_clang_unittest(CIRUnitTests | ||
PointerLikeTest.cpp | ||
LLVM_COMPONENTS | ||
Core | ||
|
||
LINK_LIBS | ||
MLIRCIR | ||
CIROpenACCSupport | ||
MLIRIR | ||
MLIROpenACCDialect | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Silly question perhaps, why is this not part of MLIR? It doesn't seem to refer to CIR at all? Or am I missing something?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe this clarifies it a bit?
https://mlir.llvm.org/docs/Interfaces/#external-models-for-attribute-operation-and-type-interfaces