[flang] [clang-tools-extra] [clang] [llvm] [flang] add SYSTEM runtime and lowering intrinsics support (PR #74309)

Yi Wu via cfe-commits cfe-commits at lists.llvm.org
Mon Jan 29 06:26:33 PST 2024


================
@@ -5934,6 +5938,40 @@ IntrinsicLibrary::genSum(mlir::Type resultType,
                       resultType, args);
 }
 
+// SYSTEM
+void IntrinsicLibrary::genSystem(llvm::ArrayRef<fir::ExtendedValue> args) {
+  assert(args.size() == 2);
+  mlir::Value command = fir::getBase(args[0]);
+  const fir::ExtendedValue &exitstat = args[1];
+
+  if (!command)
+    fir::emitFatalError(loc, "expected COMMAND parameter");
+
+  mlir::Type boxNoneTy = fir::BoxType::get(builder.getNoneType());
+
+  mlir::Value waitBool = builder.createBool(loc, true);
+  mlir::Value exitstatBox =
+      isStaticallyPresent(exitstat)
+          ? fir::getBase(exitstat)
+          : builder.create<fir::AbsentOp>(loc, boxNoneTy).getResult();
----------------
yi-wu-arm wrote:

After some testing:
```fortran
program p
character(20) :: command
command = "echo hi"

call all_args(command)

contains
  subroutine all_args(command, exitstat)
  CHARACTER(*), OPTIONAL :: command
  INTEGER, OPTIONAL :: exitstat
  call system(command, exitstat)
  end subroutine all_args
end
```
add force print out in `genSystem` in `intrinsics.Callcpp` 
```cpp
  if (isStaticallyPresent(exitstat)) {
  // if (hasAbsentOptional(exitstat)) {
    // mlir::Value isDynamicallyAbsent = builder.genIsNullAddr(loc, exitstat);
    
    exitstatBox = fir::getBase(exitstat);
    llvm::outs() << "Found: is present"
                 << ".\n";
  } else {
    llvm::outs() << "Not found: creating an absentop"
                 << ".\n";
    exitstatBox = builder.create<fir::AbsentOp>(loc, boxNoneTy).getResult();
  }
  ```
  and in runtime funrction `execute_command_line`:
  ```cpp
    if (exitstat) {
    RUNTIME_CHECK(terminator, IsValidIntDescriptor(exitstat));
    printf("exitstat pass all checks.\n");
  }else{
    printf("exitstat is null.\n");
  }
  ```
  terminal printout when testing the above fortran program:
  ```
$ ../build-release/bin/flang-new test.f90 
Found: is present.
$ ./a.out 
exitstat is null.
hi
$ 
```
Conclusion: an absent optional is `isStaticallyPresent`, but in runtime it is actually a `nullptr const Descriptor *` so it won't cause a problem because `if(nullptr)` is false. It will only be a problem on bool variable but that has been solved in another `execute_command_line` patch.
`hasAbsentOptional()` is able to detect it in `intrinsicCall.cpp`.

https://github.com/llvm/llvm-project/pull/74309


More information about the cfe-commits mailing list