The IceCube Neutrino Observatory located at the geographic South Pole is composed of two detectors. One is the in-ice optical array, which measures high-energy muons from air showers and charged particles produced by the interaction of high-energy neutrinos in the ice. The other is an array of ice-Cherenkov tanks at the surface, called IceTop, which is used both as veto for the in-ice neutrino measurements and for detecting cosmic-ray air showers. In the next decade, the IceCube-Gen2 extension will increase the surface coverage including surface radio antennas and scintillator panels on the footprint of an extended optical array in the ice. The combination of the current surface and in-ice detectors can be exploited for the study of cosmic rays and the search for PeV gamma rays. The in-ice detector measures the high-energy muonic component of air showers, whereas the signal in IceTop is dominated by the electromagnetic component. The relative size of the muonic and electromagnetic components is different for gamma-and hadron-induced air showers. Thus, the gamma-hadron separation of cosmic rays is attempted using machine learning techniques including deep learning. Here, different approaches are presented. Finally, the prospects for the detection of PeV photons with IceCube-Gen2 will be discussed.