| 
18 | 18 | #include "flang/Optimizer/Dialect/FIRAttr.h"  | 
19 | 19 | #include "flang/Optimizer/Dialect/FIROpsSupport.h"  | 
20 | 20 | #include "flang/Optimizer/Dialect/FIRType.h"  | 
 | 21 | +#include "flang/Optimizer/Support/DataLayout.h"  | 
21 | 22 | #include "flang/Optimizer/Support/FatalError.h"  | 
22 | 23 | #include "flang/Optimizer/Support/InternalNames.h"  | 
23 | 24 | #include "flang/Optimizer/Support/Utils.h"  | 
@@ -328,6 +329,17 @@ mlir::Value fir::FirOpBuilder::createHeapTemporary(  | 
328 | 329 |                                  name, dynamicLength, dynamicShape, attrs);  | 
329 | 330 | }  | 
330 | 331 | 
 
  | 
 | 332 | +mlir::Value fir::FirOpBuilder::genStackSave(mlir::Location loc) {  | 
 | 333 | +  mlir::Type voidPtr = mlir::LLVM::LLVMPointerType::get(  | 
 | 334 | +      getContext(), fir::factory::getAllocaAddressSpace(&getDataLayout()));  | 
 | 335 | +  return create<mlir::LLVM::StackSaveOp>(loc, voidPtr);  | 
 | 336 | +}  | 
 | 337 | + | 
 | 338 | +void fir::FirOpBuilder::genStackRestore(mlir::Location loc,  | 
 | 339 | +                                        mlir::Value stackPointer) {  | 
 | 340 | +  create<mlir::LLVM::StackRestoreOp>(loc, stackPointer);  | 
 | 341 | +}  | 
 | 342 | + | 
331 | 343 | /// Create a global variable in the (read-only) data section. A global variable  | 
332 | 344 | /// must have a unique name to identify and reference it.  | 
333 | 345 | fir::GlobalOp fir::FirOpBuilder::createGlobal(  | 
@@ -791,6 +803,15 @@ void fir::FirOpBuilder::setFastMathFlags(  | 
791 | 803 |   setFastMathFlags(arithFMF);  | 
792 | 804 | }  | 
793 | 805 | 
 
  | 
 | 806 | +// Construction of an mlir::DataLayout is expensive so only do it on demand and  | 
 | 807 | +// memoise it in the builder instance  | 
 | 808 | +mlir::DataLayout &fir::FirOpBuilder::getDataLayout() {  | 
 | 809 | +  if (dataLayout)  | 
 | 810 | +    return *dataLayout;  | 
 | 811 | +  dataLayout = std::make_unique<mlir::DataLayout>(getModule());  | 
 | 812 | +  return *dataLayout;  | 
 | 813 | +}  | 
 | 814 | + | 
794 | 815 | //===--------------------------------------------------------------------===//  | 
795 | 816 | // ExtendedValue inquiry helper implementation  | 
796 | 817 | //===--------------------------------------------------------------------===//  | 
@@ -1664,3 +1685,10 @@ void fir::factory::setInternalLinkage(mlir::func::FuncOp func) {  | 
1664 | 1685 |       mlir::LLVM::LinkageAttr::get(func->getContext(), internalLinkage);  | 
1665 | 1686 |   func->setAttr("llvm.linkage", linkage);  | 
1666 | 1687 | }  | 
 | 1688 | + | 
 | 1689 | +uint64_t fir::factory::getAllocaAddressSpace(mlir::DataLayout *dataLayout) {  | 
 | 1690 | +  if (dataLayout)  | 
 | 1691 | +    if (mlir::Attribute addrSpace = dataLayout->getAllocaMemorySpace())  | 
 | 1692 | +      return mlir::cast<mlir::IntegerAttr>(addrSpace).getUInt();  | 
 | 1693 | +  return 0;  | 
 | 1694 | +}  | 
0 commit comments