Skip to content

Reland [flang] In AllocMemOp lowering, convert types for calling malloc on 32-bit #130384

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

Closed
wants to merge 7 commits into from
Closed
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
8 changes: 8 additions & 0 deletions flang-rt/lib/runtime/execute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
#include "flang/Common/windows-include.h"
#else
#include <signal.h>
#ifndef __wasi__
#include <sys/wait.h>
#endif
#include <unistd.h>
#endif

Expand Down Expand Up @@ -65,6 +67,7 @@ void CheckAndStoreIntToDescriptor(
}
}

#ifndef __wasi__
// If a condition occurs that would assign a nonzero value to CMDSTAT but
// the CMDSTAT variable is not present, error termination is initiated.
std::int64_t TerminationCheck(std::int64_t status, const Descriptor *cmdstat,
Expand Down Expand Up @@ -180,6 +183,7 @@ std::int64_t TerminationCheck(std::int64_t status, const Descriptor *cmdstat,
#endif
return exitStatusVal;
}
#endif

void RTNAME(ExecuteCommandLine)(const Descriptor &command, bool wait,
const Descriptor *exitstat, const Descriptor *cmdstat,
Expand All @@ -202,6 +206,7 @@ void RTNAME(ExecuteCommandLine)(const Descriptor &command, bool wait,
RUNTIME_CHECK(terminator, IsValidCharDescriptor(cmdmsg));
}

#ifndef __wasi__
if (wait) {
// either wait is not specified or wait is true: synchronous mode
std::int64_t status{std::system(newCmd)};
Expand Down Expand Up @@ -278,6 +283,9 @@ void RTNAME(ExecuteCommandLine)(const Descriptor &command, bool wait,
}
#endif
}
#else
terminator.Crash("not supported on WASI");
#endif
// Deallocate memory if EnsureNullTerminated dynamically allocated memory
if (newCmd != command.OffsetElement()) {
FreeMemory(newCmd);
Expand Down
4 changes: 2 additions & 2 deletions flang-rt/lib/runtime/extensions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ extern "C" {
namespace Fortran::runtime {

gid_t RTNAME(GetGID)() {
#ifdef _WIN32
#if defined(_WIN32) || defined(__wasi__)
// Group IDs don't exist on Windows, return 1 to avoid errors
return 1;
#else
Expand All @@ -70,7 +70,7 @@ gid_t RTNAME(GetGID)() {
}

uid_t RTNAME(GetUID)() {
#ifdef _WIN32
#if defined(_WIN32) || defined(__wasi__)
// User IDs don't exist on Windows, return 1 to avoid errors
return 1;
#else
Expand Down
5 changes: 5 additions & 0 deletions flang-rt/lib/runtime/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ void OpenFile::set_path(OwningPtr<char> &&path, std::size_t bytes) {
}

static int openfile_mkstemp(IoErrorHandler &handler) {
#ifdef __wasi__
handler.SignalError("not supported on WASI");
return -1;
#else
#ifdef _WIN32
const unsigned int uUnique{0};
// GetTempFileNameA needs a directory name < MAX_PATH-14 characters in length.
Expand Down Expand Up @@ -58,6 +62,7 @@ static int openfile_mkstemp(IoErrorHandler &handler) {
::unlink(path);
#endif
return fd;
#endif
}

void OpenFile::Open(OpenStatus status, Fortran::common::optional<Action> action,
Expand Down
79 changes: 72 additions & 7 deletions flang/include/flang/Optimizer/Builder/FIRBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,28 @@ class BoxValue;
inline mlir::Type getIntPtrType(mlir::OpBuilder &builder) {
// TODO: Delay the need of such type until codegen or find a way to use
// llvm::DataLayout::getPointerSizeInBits here.
// (Note: this is *only* used by MemoryUtils.cpp)
return builder.getI64Type();
}

//===----------------------------------------------------------------------===//
// MinimalCTargetInfo
//===----------------------------------------------------------------------===//

/// Minimal information needed to interface with C code on the target,
/// for generating runtime calls.
struct MinimalCTargetInfo {
unsigned char CharWidth;
unsigned char ShortWidth;
unsigned char IntWidth;
unsigned char LongWidth;
unsigned char LongLongWidth;
unsigned char DataPointerWidth;
unsigned char EnumWidth;

MinimalCTargetInfo(const llvm::Triple &T);
};

//===----------------------------------------------------------------------===//
// FirOpBuilder
//===----------------------------------------------------------------------===//
Expand All @@ -57,7 +76,8 @@ class FirOpBuilder : public mlir::OpBuilder, public mlir::OpBuilder::Listener {
explicit FirOpBuilder(mlir::Operation *op, fir::KindMapping kindMap,
mlir::SymbolTable *symbolTable = nullptr)
: OpBuilder{op, /*listener=*/this}, kindMap{std::move(kindMap)},
symbolTable{symbolTable} {
symbolTable{symbolTable},
cTargetInfo{fir::getTargetTriple(getModule())} {
auto fmi = mlir::dyn_cast<mlir::arith::ArithFastMathInterface>(*op);
if (fmi) {
// Set the builder with FastMathFlags attached to the operation.
Expand All @@ -67,17 +87,20 @@ class FirOpBuilder : public mlir::OpBuilder, public mlir::OpBuilder::Listener {
explicit FirOpBuilder(mlir::OpBuilder &builder, fir::KindMapping kindMap,
mlir::SymbolTable *symbolTable = nullptr)
: OpBuilder(builder), OpBuilder::Listener(), kindMap{std::move(kindMap)},
symbolTable{symbolTable} {
symbolTable{symbolTable},
cTargetInfo{fir::getTargetTriple(getModule())} {
setListener(this);
}
explicit FirOpBuilder(mlir::OpBuilder &builder, mlir::ModuleOp mod)
: OpBuilder(builder), OpBuilder::Listener(),
kindMap{getKindMapping(mod)} {
kindMap{getKindMapping(mod)},
cTargetInfo{fir::getTargetTriple(getModule())} {
setListener(this);
}
explicit FirOpBuilder(mlir::OpBuilder &builder, fir::KindMapping kindMap,
mlir::Operation *op)
: OpBuilder(builder), OpBuilder::Listener(), kindMap{std::move(kindMap)} {
: OpBuilder(builder), OpBuilder::Listener(), kindMap{std::move(kindMap)},
cTargetInfo{fir::getTargetTriple(getModule())} {
setListener(this);
auto fmi = mlir::dyn_cast<mlir::arith::ArithFastMathInterface>(*op);
if (fmi) {
Expand All @@ -93,15 +116,17 @@ class FirOpBuilder : public mlir::OpBuilder, public mlir::OpBuilder::Listener {
: OpBuilder(other), OpBuilder::Listener(), kindMap{other.kindMap},
fastMathFlags{other.fastMathFlags},
integerOverflowFlags{other.integerOverflowFlags},
symbolTable{other.symbolTable} {
symbolTable{other.symbolTable},
cTargetInfo{other.cTargetInfo} {
setListener(this);
}

FirOpBuilder(FirOpBuilder &&other)
: OpBuilder(other), OpBuilder::Listener(),
kindMap{std::move(other.kindMap)}, fastMathFlags{other.fastMathFlags},
integerOverflowFlags{other.integerOverflowFlags},
symbolTable{other.symbolTable} {
symbolTable{other.symbolTable},
cTargetInfo{other.cTargetInfo} {
setListener(this);
}

Expand Down Expand Up @@ -160,7 +185,45 @@ class FirOpBuilder : public mlir::OpBuilder, public mlir::OpBuilder::Listener {

/// Get the integer type whose bit width corresponds to the width of pointer
/// types, or is bigger.
mlir::Type getIntPtrType() { return fir::getIntPtrType(*this); }
mlir::Type getIntPtrType() {
return getIntegerType(cTargetInfo.DataPointerWidth);
}

/// Get the integer type whose bit width corresponds to the width of
/// the `char` type in C
mlir::Type getCCharType() {
return getIntegerType(cTargetInfo.CharWidth);
}

/// Get the integer type whose bit width corresponds to the width of
/// the `short` type in C
mlir::Type getCShortType() {
return getIntegerType(cTargetInfo.ShortWidth);
}

/// Get the integer type whose bit width corresponds to the width of
/// the `int` type in C
mlir::Type getCIntType() {
return getIntegerType(cTargetInfo.IntWidth);
}

/// Get the integer type whose bit width corresponds to the width of
/// the `long` type in C
mlir::Type getCLongType() {
return getIntegerType(cTargetInfo.LongWidth);
}

/// Get the integer type whose bit width corresponds to the width of
/// the `long long` type in C
mlir::Type getCLongLongType() {
return getIntegerType(cTargetInfo.LongLongWidth);
}

/// Get the integer type whose bit width corresponds to the width of
/// enums in C
mlir::Type getCEnumType() {
return getIntegerType(cTargetInfo.EnumWidth);
}

/// Wrap `str` to a SymbolRefAttr.
mlir::SymbolRefAttr getSymbolRefAttr(llvm::StringRef str) {
Expand Down Expand Up @@ -619,6 +682,8 @@ class FirOpBuilder : public mlir::OpBuilder, public mlir::OpBuilder::Listener {
/// Stored via a unique_ptr rather than an optional so as not to bloat this
/// class when most instances won't ever need a data layout.
std::unique_ptr<mlir::DataLayout> dataLayout = nullptr;

MinimalCTargetInfo cTargetInfo;
};

} // namespace fir
Expand Down
Loading
Loading